Case Changing of Python String Methods

The below Python functions are used to change the case of the strings. Let’s look at some Python string methods with examples:

  • lower(): Converts all uppercase characters in a string into lowercase
  • upper(): Converts all lowercase characters in a string into uppercase
  • title(): Convert string to title case
  • swapcase(): Swap the cases of all characters in a string
  • capitalize(): Convert the first character of a string to uppercase

Example: Changing the case of Python String Methods

Python3




# Python3 program to show the
# working of upper() function
text = 'geeKs For geEkS'
  
# upper() function to convert
# string to upper case
print("\nConverted String:")
print(text.upper())
  
# lower() function to convert
# string to lower case
print("\nConverted String:")
print(text.lower())
  
# converts the first character to 
# upper case and rest to lower case 
print("\nConverted String:")
print(text.title())
  
# swaps the case of all characters in the string
# upper case character to lowercase and viceversa
print("\nConverted String:")
print(text.swapcase())
  
# convert the first character of a string to uppercase
print("\nConverted String:")
print(text.capitalize())
  
# original string never changes
print("\nOriginal String")
print(text)


Output

Converted String:
GEEKS FOR GEEKS

Converted String:
geeks for geeks

Converted String:
Geeks For Geeks

Converted String:
GEEkS fOR GEeKs

Original String
geeKs For geEkS

Time complexity: O(n) where n is the length of the string ‘text’
Auxiliary space: O(1)

Python String Methods

Python string methods is a collection of in-built Python functions that operates on lists.

Note: Every string method in Python does not change the original string instead returns a new string with the changed attributes. 

Python string is a sequence of Unicode characters that is enclosed in quotation marks. In this article, we will discuss the in-built string functions i.e. the functions provided by Python to operate on strings.

Similar Reads

Case Changing of Python String Methods

The below Python functions are used to change the case of the strings. Let’s look at some Python string methods with examples:...

List of String Methods in Python

...