Class 12 : Python String Inbuilt Functions 4

casefold()

The casefold() method returns a string where all the characters are lower case.

Syntax:
string.casefold()

Make the string lower case:

Example:

#casefold()
n="heLLO"
print(n.casefold())
n="hello ARE You"
print(n.casefold())
n="123 HELLO"
print(n.casefold())

Output:

hello
hello are you
123 hello
>>>

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)

Example:

#center
#Using the letter “*” as the padding character:
txt = “catalyst”
x = txt.center(20, “*”)
print(x)

#center
#Using the letter "*" as the padding character:
txt = "catalyst"
x = txt.center(20, "*")
print(x)

output:

******catalyst******

count()

The count() method returns the number of times a specified value appears in the string.

Syntax
string.count(value, start, end)

Example:1

#count() 
#Return the number of times the value "apple" appears in the string:
Example:
n = "I love apples, apple are my favourite fruit"
x = n.count("apple")
print(x)

Output:

2

Example:2

#Example
#Search from position 10 to 24:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple", 10, 24)
print(x)

Output:

1

find()

The find() method finds the first occurrence of the specified value.
The find() method returns -1 if the value is not found.
The find() method is almost the same as the index() method, the only difference is that the index() method raises an exception if the value is not found.

Syntax
string.find(value, start, end)

Example:

#find() 
#Where in the text is the word "welcome"?:
txt = "Hello, welcome to my world."
x = txt.find("welcome")
print(x)

Output:

7
(Counting starts from 0)

Example:2

#find() 
#Where in the text is the first occurrence of the letter "e"?:
txt = "Hello, welcome to my world."
x = txt.find("e")
print(x) 

Output:

1