Python
Dr.G.Murugeswari
Asso.Prof.
CSE Dept.,MSU
String Functions
Built-in methods
Checking the presence of Alphabet,digit,decimal and
alphanumeric in a String
• isalpha() Returns True if all characters in the
string are in the alphabet
• isdigit() Returns True if all characters in the string
are digits
• isdecimal() Returns True if all characters in the
string are decimals
• isalnum() Returns True if all characters in the
string are alphanumeric
• isnumeric() Returns True if all characters in the
string are numeric
String Checking
• islower() Returns True if all characters in the
string are lower case
• isspace() Returns True if all characters in the
string are whitespaces
• isupper() Returns True if all characters in the
string are upper case
• isidentifier() Returns True if the string is an
identifier
• istitle() Returns True if the string follows the
rules of a title
• isprintable() Returns True if all characters in the
string are printable
String Conversion
• upper()Converts a string into upper case
string.upper()
• lower() Converts a string into lower case
string.lower()
txt = "hello, and welcome to my world.“
x = txt.capitalize()
• capitalize()
print (x) Converts the
txt = "Hello, And firstTocharacter
Welcome My World!" to
upper case
x = txt.casefold()
print(x)
• casefold()Converts string into lower case
x= hello, and welcome to my world!
String Conversion
• swapcase()Swaps cases, lower case becomes
upper case and vice versa
txt = "Hello My Name Is PETER"
x = txt.swapcase()
print(x) hELLO mY nAME iS peter
• title() Converts the first character of each
word to upper case
String Functions
• ljust() Returns a left justified version of the
string
• rjust()Returns a right justified version of the
string
• lstrip()Returns a left trim version of the string
• rstrip()Returns a right trim version of the
string
String Functions
• format() Formats specified values in a string
• endswith() Returns true if the string ends
with the specified value
txt = "Hello, welcome to my world."
x = txt.endswith(".")
print(x) True
• expandtabs() Sets the tab size of the string
txt = "H\te\tl\tl\to"
x = txt.expandtabs(2)
print(x) Hello
String Functions
• center() Returns a centered string
• count() Returns the number of times a
specified value occurs in a string
• encode() Returns an encoded version of the
string
• find()Searches the string for a specified value
and returns the position of where it was found
String Functions
• rfind()Searches the string for a specified value
and returns the last position of where it was
found
txt = "Mi casa, su casa."
x = txt.rfind("casa")
print(x) 12
• replace() Returns a string where a specified
value isbananas"
txt = "I like replaced with a another value
x = txt.replace("bananas", "apples")
print(x) I like apples
String Functions
• join()Joins the elements of an iterable to the
end of the string
myTuple = ("John", "Peter", "Vicky")
x = "#".join(myTuple)
print(x) John#Peter#Vicky
myDict = {"name": "John", "country": "Norway"}
mySeparator = "TEST"
x = mySeparator.join(myDict)
print(x) nameTESTcountry
String Functions
• index()Searches the string for a specified value
and returns the position of where it was found
txt = "Hello, welcome to my world."
x = txt.index("welcome")
print(x) 7
• rindex()Searches the string for a specified
value and returns the last position of where it
was
txt = "Mifound
casa, su casa."
x = txt.rindex("casa")
• join()Joins
print(x) the elements of an 12iterable to the
end of the string
String Functions
• translate() Returns a translated string
#use a dictionary with ascii codes to replace 83 (S) with 80 (P):
mydict = {83: 80}
txt = "Hello Sam!"
print(txt.translate(mydict)) Hello Pam!
• startswith() Returns true if the string starts
with the specified value
txt = "Hello, welcome to my world."
x = txt.startswith("Hello")
print(x) True
String Functions
• strip() Returns a trimmed version of the string
txt = " banana "
x = txt.strip()
print("of all fruits", x, "is my favorite") of all fruits banana is my favorite
• split()Splits the string at the specified
separator, and returns a list
txt = "welcome to the jungle"
x = txt.split()
print(x) ['welcome', 'to', 'the', 'jungle']
String Functions
• rsplit()Splits the string at the specified
separator, and returns a list
txt = "apple, banana, cherry"
x = txt.rsplit(", ")
print(x) ['apple', 'banana', 'cherry']
• splitlines()Splits the string at line breaks and
returns a list
txt = "Thank you for the music\nWelcome to the jungle"
x = txt.splitlines()
print(x) ['Thank you for the music', 'Welcome to the jungle']