Class 12 : Python String Inbuilt Functions

isalpha()

This method helps us to check whether all the characters in string are alphabet letters (a-z,A-Z) or not .
Example of characters that are not alphabet letters:
(space)!#%&? etc.
Syntax
string.isalpha()

Returns “True” if all the characters in the text are alphabets otherwise returns “False”.

Example:

#isalpha()
n="hello"
print(n.isalpha())
n="hello123"
print(n.isalpha())
x=n.isalpha()
print(x)

Output:

True
False
False
>>>

islower()

This method helps us to check whether all the characters in string are lower case alphabet letters (a-z) or not .
Syntax
string.islower()

Returns “True” if all the characters in the text are lower case alphabets otherwise returns “False”.

Example:

#islower()
n="hello"
print(n.islower())
n="Hello"
print(n.islower())
x=n.islower()
print(x)

Output:

True
False
False
>>>

isupper()

This method helps us to check whether all the characters in string are upper case alphabet letters (A-Z) or not .
Syntax
string.isupper()

Returns “True” if all the characters in the text are upper case alphabets otherwise returns “False”.

Example:

#isupper()
 n="hello"
 print(n.isupper())
 n="HELLO"
 print(n.isupper())
 x=n.isupper()
 print(x)

Output:

False
True
True
>>>

isdigit()

This method helps us to check whether all the characters in string are digits (0-9) or not .
Syntax
string.isdigit()

Returns “True” if all the characters in the text are digits otherwise returns “False”.

Example:

#isdigit()
 n="12345"
 print(n.isdigit())
 n="HELLO12"
 print(n.isdigit())
 x=n.isdigit()
 print(x)

Output:

True
False
False
>>>

isspace()

This method helps us to check whether all the characters in string are spaces or not .
Syntax
string.isspace()

Returns “True” if all the characters in the text are spaces otherwise returns “False”.

Example:

#isspace()
 n=" "
 print(n.isspace())
 n="HELLO"
 print(n.isspace())
 x=n.isspace()
 print(x)

Output:

True
False
False
>>>

istitle()

The istitle() method returns True if all words in a text start with a upper case letter, AND the rest of the word are lower case letters, otherwise False.
Symbols and numbers are ignored.
Syntax
string.istitle()

Example:

#istitle()
 n="Hello How Are You"
 print(n.istitle())
 n="HELLO"
 print(n.istitle())
 n="hello World"
 x=n.istitle()
 print(x)

Output:

True
False
False
>>>

lower()

This method converts all upper case alphabet characters of string to lower case.
Symbols and Numbers are ignored.

Syntax
string.lower()

Example:

#lower()
 n="Hello How Are You"
 print(n.lower())
 n="HELLO"
 print(n.lower())
 n="hello World"
 x=n.lower()
 print(x)

Output:

hello how are you
hello
hello world
>>>

upper()

This method converts all lower case alphabet characters of string to upper case.
Symbols and Numbers are ignored.

Syntax
string.upper()

Example

#upper()
 n="Hello How Are You"
 print(n.upper())
 n="HELLO"
 print(n.upper())
 n="hello World"
 x=n.upper()
 print(x)

Output:

HELLO HOW ARE YOU
HELLO
HELLO WORLD
>>>

title()

This method helps to convert 1st character of each word to upper case rest are converted to lower case.
If the word contains a number or a symbol, the first letter after that will be converted to upper case.

Syntax
string.title()

Example:

#title()
 n="Hello How Are You"
 print(n.title())
 n="HELLO"
 print(n.title())
 n="hello World"
 x=n.title()
 print(x)

Output:

Hello How Are You
Hello
Hello World
>>>

swapcase()

This method helps us to convert all upper case alphabets to lower case and all lower case alphabets to upper case.
Syntax
string.swapcase()
Example:

#swapcase()
 n="Hello How Are You"
 print(n.swapcase())
 n="HELLO"
 print(n.swapcase())
 n="Hello World"
 x=n.swapcase()
 print(x)

Output:

hELLO hOW aRE yOU
hello
hELLO wORLD
>>>

join()

The method takes all items in an iterable and joins them into one string.
A string must be specified as the separator.
Syntax
string.join(iterable)

Example:

#join()
 print("Example of list")
 n=["Hello","How","Are","You"]
 z="".join(n)
 print(z)
 z="#".join(n)
 print(z)
 print("Example of tuple")
 n=("HELLO","HOW","ARE","YOU")
 z="".join(n)
 print(z)
 z="#".join(n)
 print(z)
 print("Example of dictionary")
 n={"roll":"1001","name":"amit","per":"98"}
 z="".join(n)
 print(z)
 z="#".join(n)
 print(z)
 myDict = {"name": "Amit", "country": "India"}
 separator = ":"
 x = separator.join(myDict)
 print(x)

Output:

Example of list
HelloHowAreYou
Hello#How#Are#You
Example of tuple
HELLOHOWAREYOU
HELLO#HOW#ARE#YOU
Example of dictionary
rollnameper
roll#name#per
name:country
>>>

capitalize()

The capitalize() method returns a string where the first character is upper case.
Syntax
string.capitalize()

Example:

#capitalize()
 n="hello"
 print(n.capitalize())
 n="hello are you"
 print(n.capitalize())
 n="123 hello"
 print(n.capitalize())

Output:

Hello
Hello are you
123 hello
>>>

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)

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

Python String Inbuilt Function       Python Math Inbuilt Function       Python Data and Time Inbuilt Functions       Python Random Module Functions

Number Of Visitors

0125693