Unit01_String Functions
Unit01_String Functions
Unit01_String Functions
Python has a set of built-in methods that you can use on strings.
Method Description
endswith() Returns true if the string ends with the specified value
find() Searches the string for a specified value and returns the position of where it
was found
index() Searches the string for a specified value and returns the position of where it
was found
isalpha() Returns True if all characters in the string are in the alphabet
isascii() Returns True if all characters in the string are ascii characters
islower() Returns True if all characters in the string are lower case
isupper() Returns True if all characters in the string are upper case
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with a specified value
rfind() Searches the string for a specified value and returns the last position of
where it was found
rindex() Searches the string for a specified value and returns the last position of
where it was found
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
split() Splits the string at the specified separator, and returns a list
startswith() Returns true if the string starts with the specified value
swapcase() Swaps cases, lower case becomes upper case and vice versa
zfill() Fills the string with a specified number of 0 values at the beginning
1. Python String capitalize() Method:
x = txt.capitalize()
print (x)
x = txt.casefold()
print(x)
x = txt.center(20)
print(x)
x = txt.count("apple")
print(x)
5. Python String encode() Method
UTF-8 encode the string:
txt = "My name is Ståle"
x = txt.encode()
print(x)
x = txt.endswith(".")
print(x)
x = txt.expandtabs(2)
print(x)
x = txt.find("welcome")
print(x)
9. Python String format() Method
Insert the price inside the placeholder, the price should be in fixed point, two-decimal format:
txt = "For only {price:.2f} dollars!"
print(txt.format(price = 49))
x = txt.index("welcome")
print(x)
x = txt.isalnum()
print(x)
x = txt.isalpha()
print(x)
13. Python String isascii() Method
Check if all the characters in the text are ascii characters:
txt = "Company123"
x = txt.isascii()
print(x)
x = txt.isdecimal()
print(x)
x = txt.isdigit()
print(x)
x = txt.isidentifier()
print(x)
17. Python String islower() Method
Check if all the characters in the text are in lower case:
txt = "hello world!"
x = txt.islower()
print(x)
x = txt.isnumeric()
print(x)
x = txt.isprintable()
print(x)
x = txt.isspace()
print(x)
21. Python String istitle() Method
Check if each word start with an upper case letter:
txt = "Hello, And Welcome To My World!"
x = txt.istitle()
print(x)
x = txt.isupper()
print(x)
x = "#".join(myTuple)
print(x)
x = txt.ljust(20)
x = txt.lower()
26. Python String lstrip() Method
Remove spaces to the left of the string:
txt = " banana "
x = txt.lstrip()
x = txt.partition("bananas")
print(x)
x = txt.replace("bananas", "apples")
print(x)
30. Python String rfind() Method
Where in the text is the last occurrence of the string "casa"?:
txt = "Mi casa, su casa."
x = txt.rfind("casa")
print(x)
x = txt.rindex("casa")
print(x)
x = txt.rjust(20)
x = txt.rpartition("bananas")
print(x)
34. Python String rsplit() Method
Split a string into a list, using comma, followed by a space (, ) as the separator:
txt = "apple, banana, cherry"
x = txt.rsplit(", ")
print(x)
x = txt.rstrip()
x = txt.split()
print(x)
x = txt.splitlines()
print(x)
x = txt.strip()
x = txt.swapcase()
print(x)
x = txt.title()
print(x)
x = txt.zfill(10)
print(x)