Python inbuilt String Functions

swapcase()

The swapcase() method returns a string where all the upper case letters are lower case and vice versa.

Syntax
string.swapcase()

Make the lower case letters upper case and the upper case letters lower case

n="Hello"
print(n.swapcase())

n="HELLO"
print(n.swapcase())

n="CoMpUtEr"
print(n.swapcase())
#Output

hELLO
hello
cOmPuTeR

casefold()

The casefold() method returns a string where all the characters are lower case.
This method is similar to the lower() method, but the casefold() method is stronger, more aggressive, meaning that it will convert more characters into lower case, and will find more matches when comparing two strings and both are converted using the casefold() method.

Syntax
string.casefold()

Make the string lower case

n="Hello"
print(n.casefold())

n="HELLO"
print(n.casefold())

n="CoMpUtEr"
print(n.casefold())
#Output

hello
hello   
computer

center()

The center() method will center align the string, using a specified character (space is default) as the fill character.

Syntax
string.center(length, character)

length : The length of the returned string
character :The character to fill the missing space on each side. Default is ” ” (space)(Optional)

n=”Computer”
print(n.center(11,’*’))

Using the letter “*” as the padding character

n="Hello"
print(n.center(5,'*'))

n="HELLO"
print(n.center(7,'*'))

n="Computer"
print(n.center(11,'*'))

n="Computer"
print(n.center(12,'*'))
#Output

Hello
*HELLO*
**Computer*
**Computer**

Python String inbuilt Methods       Python Math inbuilt Methods       Python Date And Time inbuilt Methods      Python Random inbuilt Methods