Skip to content

How to change case of string in Python

How to change case of string in python

In this tutorial we will see how to change case of string in Python . Python string has various methods for changing the case of any string . They are as follows –

  • Python string upper() method
  • Python string lower() method
  • Python string capitalize() method
  • Python string title() method
  • Python string swapcase() method
  • Python string casefold() method

Now we will discuss about all the method with examples –

Python string upper() method

It will return a copy of the string where all the character of the string will be converted to uppercase .

Syntax of upper() method

string.upper()

Parameters of upper() method

It does not have any parameter .

Return type of upper() method

It returns a string .

Examples of Python String upper() method

Example 1: How to convert all character of a string to uppercase in Python

mystring = "CodiNg ConCeptioN"
print(mystring.upper())
CODING CONCEPTION
  • In the above code first we have declared a variable mystring as "CodiNg ConCeptioN" .
  • After that we have converted all the character of string to uppercase using upper() method and printed it .

Example 2: How to convert a given character of a string to uppercase in Python

mystring = "CodiNg ConCeptioN"
newstring=""

# converting all i into uppercase
for letter in mystring:
    if letter=="i":
        newstring += letter.upper()
    else:
        newstring += letter
    
print(newstring)    
CodINg ConCeptIoN
  • In the above code first we have declared a variable mystring and newstring as "CodiNg ConCeptioN" and ""
  • Then we have iterated all the character of mystring using for-loop and added it in newstring . When "i" is found it is converted into "I" using upper() method before adding .
  • After that we have printed newstring .

Python string lower() method

It will return a copy of the string where all the character of the string will be converted to lowercase .

Syntax of lower() method

string.lower()

Parameters of lower() method

It does not have any parameter .

Return type of lower() method

It returns a string .

Examples of Python String lower() method

Example 1: How to convert all character of a string to lowercase in Python

mystring = "CodiNg ConCeptioN"
print(mystring.lower())
coding conception
  • In the above code first we have declared a variable mystring as "CodiNg ConCeptioN" .
  • After that we have converted all the character of string to lowercase using lower() method and printed it .

Example 2: How to convert a given character of a string to lowercase in Python

mystring = "CodiNg ConCeptioN"
newstring=""

# converting all C into lowercase
for letter in mystring:
    if letter=="C":
        newstring += letter.lower()
    else:
        newstring += letter
    
print(newstring)    
codiNg conceptioN
  • In the above code first we have declared a variable mystring and newstring as "CodiNg ConCeptioN" and ""
  • Then we have iterated all the character of mystring using for-loop and added it in newstring . When "C" is found it is converted into "c" using lower() method before adding .
  • After that we have printed newstring .

Python string capitalize() method

It will return a copy of the string where all the character of the string will be converted to lowercase except the first character .

Syntax of capitalize() method

string.capitalize()

Parameters of capitalize() method

It does not have any parameter .

Return type of capitalize() method

It returns a string .

Examples of Python String capitalize() method

Example 1:

mystring = "CodiNg ConCeptioN"
print(mystring.capitalize())
Coding conception
  • In the above code first we have declared a variable mystring as "CodiNg ConCeptioN" .
  • After that we have converted all the character of string to lowercase except the first character using capitalize() method and printed it .
  • From the output we can see that only the first character of the string is in uppercase and rest all is in lowercase

Python string title() method

It will return a copy of the string where all the character of the string will be converted to lowercase except the first character of each word .

Syntax of title() method

string.title()

Parameters of title() method

It does not have any parameter .

Return type of title() method

It returns a string .

Examples of Python String title() method

Example 1:

mystring = "CodiNg ConCeptioN"
print(mystring.title())
Coding Conception
  • In the above code first we have declared a variable mystring as "CodiNg ConCeptioN" .
  • After that we have converted all the character of string to lowercase except the first character of each word using title() method and printed it .
  • From the output we can see that only the first character of each word of the string is in uppercase and rest all is in lowercase

Python string swapcase() method

It will return a copy of the string where all the uppercase character of the string will be converted to lowercase and all the lowercase character of the string will be converted to uppercase .

Syntax of swapcase() method

string.swapcase()

Parameters of swapcase() method

It does not have any parameter .

Return type of swapcase() method

It returns a string .

Examples of Python String swapcase() method

Example 1:

mystring = "CodiNg ConCeptioN"
print(mystring.swapcase())
cODInG cONcEPTIOn
  • In the above code first we have declared a variable mystring as "CodiNg ConCeptioN" .
  • After that we have converted all the uppercase character of string to lowercase and all the lowercase character of string to uppercase using swapcase() method and printed it .

Python string casefold() method

It will return a copy of the string where all the character of the string will be converted to lowercase .

Syntax of casefold() method

string.casefold()

Parameters of casefold() method

It does not have any parameter .

Return type of casefold() method

It returns a string .

Examples of Python String casefold() method

Example 1:

mystring = "CodiNg ConCeptioN"
print(mystring.casefold())
coding conception
  • In the above code first we have declared a variable mystring as "CodiNg ConCeptioN" .
  • After that we have converted all the character of string to lowercase using casefold() method and printed it .

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply