Hàm center() trong Python

Python has many built-in methods that perform operations on strings. One of the operations is to change the case of letters. We'll see different ways to change the case of the letters in a string, and go through many examples in each section, and finally go through some of the applications.

Nội dung chính

  • Different cases
  • Capitalize the first letter using capitalize[]
  • Convert the entire string to upper-case
  • Convert the entire string to lower-case
  • Capitalize first letter of each word
  • Applications
  • Searching names
  • E-mail addresses

Python strings are immutable, which means that once created these strings cannot be modified. As a consequence, most of the functions that operate on strings actually return a modified copy of the string; the original string is unchanged.

Let's take a look at some of the ways we can manipulate strings to change the case of words.

Different cases

We have covered different cases like:

  • Capitalize the first letter using capitalize[]
  • Convert the entire string to upper-case
  • Convert the entire string to lower-case
  • Capitalize first letter of each word

Capitalize the first letter using capitalize[]

To capitalize the first letter, use the

str1 = "hello world!"
str2 = "hello Python!"
print [str1.capitalize[]]
print [str2.capitalize[]]
6 function. This functions converts the first character to uppercase and converts the remaining characters to lowercase. It doesn't take any parameters and returns the copy of the modified string.

Syntax:

str1 = "hello world!"
str2 = "hello Python!"
print [str1.capitalize[]]
print [str2.capitalize[]]
7

Example:

s = "hello openGeNus"
t = s.capitalize[]
print[t]
'Hello opengenus'

Notice that the function

str1 = "hello world!"
str2 = "hello Python!"
print [str1.capitalize[]]
print [str2.capitalize[]]
6 returned a modified copy of the original string. This means that our original string
str1 = "hello world!"
str2 = "hello Python!"
print [str1.capitalize[]]
print [str2.capitalize[]]
9 was not modified.
The first letter 'h' was converted to 'H'. Further an upper-case letter 'G' was converted to its lower-case letter 'g' and similarly 'N' was converted to 'n'

Convert the entire string to upper-case

To convert all the letters of the string to uppercase, we can use the

Hello world!
Hello python!
0 function. The function does not take any parameters and returns the copy of modified string with all letters converted to upper-case.

Syntax:

Hello world!
Hello python!
1

Example:

s = "hello openGeNus"
t = s.upper[]
print[t]
'HELLO OPENGENUS'

Convert the entire string to lower-case

To convert all the letters of the string to lowercase, we can use the

Hello world!
Hello python!
2 function. This function does not take any parameters and converts all the letters to lowercase.

Syntax:

Hello world!
Hello python!
3

Example:

str1 = "hello world!"
str2 = "hello Python!"
print [str1.capitalize[]]
print [str2.capitalize[]]
0
str1 = "hello world!"
str2 = "hello Python!"
print [str1.capitalize[]]
print [str2.capitalize[]]
1

Capitalize first letter of each word

To capitalize the first letter of each word use the

Hello world!
Hello python!
4 function. This function does not take any parameters and converts the first letter of each word to uppercase and rest of the letters to lowercase and returns the modified copy of the string.

Syntax:

Hello world!
Hello python!
5

Example:

str1 = "hello world!"
str2 = "hello Python!"
print [str1.capitalize[]]
print [str2.capitalize[]]
2
str1 = "hello world!"
str2 = "hello Python!"
print [str1.capitalize[]]
print [str2.capitalize[]]
3

Notice that while each word was capitalized, the rest of the letters were converted to lowercase.

Applications

Now that we know how to perform basic case-manipulation, lets see how can this be useful for us?
There are many simple applications, we'll go through some of the basic ones.

Prompts

A prompt is a text message that prompts the user to enter some input. For example, a prompt to enter a number.

A simple prompt could be to allow user to provide a permission by typing the word 'Yes'. In python "Yes", "yeS" or "YES" are all distinct strings, and we need to be able to accept all variants of those. We can write code to check each of those individually, but the

Hello world!
Hello python!
2 or
Hello world!
Hello python!
0 function provides an easy way.

Let's go through some sample code

str1 = "hello world!"
str2 = "hello Python!"
print [str1.capitalize[]]
print [str2.capitalize[]]
4

Here, we checked for input 'Yes' and every other input would be considered as a 'No'. This is sometimes useful, but adding a check for 'No' might be required; especially when you want to provide additional messages when user types invalid inputs like a number, instead of 'Yes' or 'No'.

Searching names

Usually, to speed up searching process, names are better stored in the same case. This is done only when the changing the case would not cause any problems. The reason why you'd want to do this could be to make searching faster. Searching for someone named Alice as "ALice", "ALICE" or "alice" could be confusing and lead to unnecessary work.

To overcome this problem, names could be stored in upper-case by using the

Hello world!
Hello python!
0 or in lowercase using the
Hello world!
Hello python!
2 function. This would allow for faster searches because now you don't have to convert all the names while you perform searches which can be time consuming if there are many names or if the search is occurring multiple times. This is usually done in a students list of names, where case of the names would not matter.

E-mail addresses

E-mail addresses are not case-sensitive. So in an application that requires sign in, you have to be able to handle all types of email address inputs.

Email addresses like "", "" or "" are all same email addresses, but different python strings. Thus the input needs to be converted to lower-case, which is common format for email addresses.

Here, the

Hello world!
Hello python!
2 function would be useful.

str1 = "hello world!"
str2 = "hello Python!"
print [str1.capitalize[]]
print [str2.capitalize[]]
5

here, we have assumed that there is a function

str.capitalize[]
1 that searches for
str.capitalize[]
2 and returns a boolean value depending on whether or not the email was found.

Chủ Đề