Online Tutorials & Training Materials | STechies.com
Register Login

Python: String Capitalize() Method

18 Feb 2020 11:35 am || 0

While working with strings, you might come across different types of strings. These strings might be obtained from the users as an input. The strings may have mixed cases - uppercase and lowercase characters. You may have to capitalize the first characters of a string.       

In Python, when we need the first character of any given string in uppercase or capital letter and rest of the character present in the original string in lowercase or small letter, then we use python capitalize() function.

Syntax:

string_name.capitalize() 

Parameters: It doesn't take any parameters.

Returns: 

  • If the first character of the original string is already uppercase then it will return the original string.
  • 2. If the first character is non-character, it will leave the first character as it is. All the other characters present in the original string will be converted into lowercase.
  • 3. This function will not change the value of the original variable.

capitalize() method can have many possibilities ranging from ‘all lower case string’ to ‘ non alphabetic  first character’ let us discuss them all one by one.

Capitalize a String in Python

Example 1: capitalize() Method with all Lowercase Characters in String

# Python code to explain capitalize() function 

# Initialize a string with all lowercase characters
str1 = 'smart techies'

# Pass strings with capitalize() function
capstr1 = str1.capitalize()

# Print output
print('Orignal String: ', str1, '\nNew String:     ',capstr1)

Output:

Orignal String:  smart techies
New String:      Smart techies

In the above code, we initialized the string ‘str1’ with all lower case characters. When we used the capitalize() method in ‘str1’ what it did was. It converted the first character of the string to upper case.

And the rest of the string remains unchanged since all the characters were in lower cases. Thus we got the output ‘Smart techies’.

Example 2: Capitalize() Method With First Character in Upper Case in String

# Python code to explain capitalize() function

# Initialize a string with first uppercase character in string
str2 = 'Smart techies'

# Pass strings with capitalize() function
capstr1 = str1.capitalize()

# Print output
print('Orignal String: ', str1, '\nNew String:     ',capstr1)

Output:

Orignal String:  Smart techies
New String:      Smart techies

We know the capitalize() method converts only the first character of the string to upper case. And in this example, the first character is already in an upper case while the rest of the string is in lower cases.

Thus we don’t see any changes in the new string.

Example 3: Capitalize() Method With All Character in Upper Case in String

# Python code to explain capitalize() function
  
# Initialize a string with all uppercase character
str1 = 'SMART TECHIES'

# Pass strings with capitalize() function
capstr1 = str1.capitalize()

# Print output
print('Orignal String: ', str1, '\nNew String:     ',capstr1)

Output:

Orignal String:  SMART TECHIES
New String:      Smart techies

In the above code, we initialized the string ‘str1’ with all upper case characters. When we used capitalize() method in ‘str1’ what it did was. It converted all the characters in upper cases to lower cases except the first character.

Thus we got the output ‘Smart techies

Example 4: capitalize() Method with Non-alphabetic First Character in String

# Python code to explain capitalize() function

# Initialize a string with Non-alphabetic First Character
str1 = '#SMART Techies'

# Pass strings with capitalize() function
capstr1 = str1.capitalize()

# Print output
print('Orignal String: ', str1, '\nNew String:     ',capstr1)

Output:

Orignal String:  #SMART Techies
New String:      #smart techies

In the above code, we initialized the string ‘str1’ whose first character is non-alphabetic. In this case, it is a ‘hash’ symbol. So when we use capitalize() method in ‘str1’. It doesn’t change the first character but changes the rest of the string to lower case.

Thus we got the output  ‘#smart techies’.

All Example in one code block

Example:

# Python code to explain capitalize() function 
  
# Initialize a string with all lowercase characters
str1 = 'smart techies'

# Initialize a string with first uppercase character
str2 = 'Smart techies'

# Initialize a string with all uppercase character
str3 = 'SMART TECHIES'

# Initialize a string with first numeric character
str4 = '1SMART Techies'  

# Pass strings with capitalize() function
capstr1 = str1.capitalize()
capstr2 = str2.capitalize()
capstr3 = str3.capitalize()
capstr4 = str4.capitalize()

# Print output
print('String 1: ', str1, ' New 1: ',capstr1)
print('String 2: ', str2, ' New 2: ',capstr2)
print('String 3: ', str3, ' New 3: ',capstr3)
print('String 4: ', str4, ' New 4: ',capstr4)

Output:

Old String 1:  smart techies  New String 1:  Smart techies
Old String 2:  Smart techies  New String 2:  Smart techies
Old String 3:  SMART TECHIES  New String 3:  Smart techies
Old String 4:  1SMART Techies  New String 4:  1smart techies

Conclusion

The capitalize() is great for modifying the case of the string characters without many hassles. As the original string is not modified, you can perform operations and experiment with the new string that the method returns.