Unit01_String Functions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Python String Methods

Python has a set of built-in methods that you can use on strings.

Method Description

capitalize() Converts the first character to upper case

casefold() Converts string into lower case

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

endswith() Returns true if the string ends with the specified value

expandtabs() Sets the tab size of the string

find() Searches the string for a specified value and returns the position of where it
was found

format() Formats specified values in a string

format_map() Formats specified values in a string

index() Searches the string for a specified value and returns the position of where it
was found

isalnum() Returns True if all characters in the string are alphanumeric

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

isdecimal() Returns True if all characters in the string are decimals

isdigit() Returns True if all characters in the string are digits


isidentifier() Returns True if the string is an identifier

islower() Returns True if all characters in the string are lower case

isnumeric() Returns True if all characters in the string are numeric

isprintable() Returns True if all characters in the string are printable

isspace() Returns True if all characters in the string are whitespaces

istitle() Returns True if the string follows the rules of a title

isupper() Returns True if all characters in the string are upper case

join() Converts the elements of an iterable into a string

ljust() Returns a left justified version of the string

lower() Converts a string into lower case

lstrip() Returns a left trim version of the string

maketrans() Returns a translation table to be used in translations

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

rjust() Returns a right justified version of the string

rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list

rstrip() Returns a right trim version of the string

split() Splits the string at the specified separator, and returns a list

splitlines() Splits the string at line breaks and returns a list

startswith() Returns true if the string starts with the specified value

strip() Returns a trimmed version of the string

swapcase() Swaps cases, lower case becomes upper case and vice versa

title() Converts the first character of each word to upper case

translate() Returns a translated


string

upper() Converts a string into upper case

zfill() Fills the string with a specified number of 0 values at the beginning
1. Python String capitalize() Method:

#Upper case the first letter in this sentence:


txt = "hello, and welcome to my world."

x = txt.capitalize()

print (x)

2. Python String casefold() Method


#Make the string lower case:
txt = "Hello, And Welcome To My World!"

x = txt.casefold()

print(x)

3. Python String center() Method


Print the word "banana", taking up the space of 20 characters, with "banana" in the middle:
txt = "banana"

x = txt.center(20)

print(x)

4. Python String count() Method


Return the number of times the value "apple" appears in the string:
txt = "I love apples, apple are my favorite fruit"

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)

6. Python String endswith() Method


Check if the string ends with a punctuation sign (.):
txt = "Hello, welcome to my world."

x = txt.endswith(".")

print(x)

7. Python String expandtabs() Method


Set the tab size to 2 whitespaces:
txt = "H\te\tl\tl\to"

x = txt.expandtabs(2)

print(x)

8. Python String find() Method


Where in the text is the word "welcome"?:
txt = "Hello, welcome to my world."

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))

10. Python String index() Method


Where in the text is the word "welcome"?:
txt = "Hello, welcome to my world."

x = txt.index("welcome")

print(x)

11. Python String isalnum() Method


Check if all the characters in the text are alphanumeric:
txt = "Company12"

x = txt.isalnum()

print(x)

12. Python String isalpha() Method


Check if all the characters in the text are letters:
txt = "CompanyX"

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)

14. Python String isdecimal() Method


Check if all the characters in a string are decimals (0-9):
txt = "1234"

x = txt.isdecimal()

print(x)

15. Python String isdigit() Method


Check if all the characters in the text are digits:
txt = "50800"

x = txt.isdigit()

print(x)

16. Python String isidentifier() Method


Check if the string is a valid identifier:
txt = "Demo"

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)

18. Python String isnumeric() Method


Check if all the characters in the text are numeric:
txt = "565543"

x = txt.isnumeric()

print(x)

19. Python String isprintable() Method


Check if all the characters in the text are printable:
txt = "Hello! Are you #1?"

x = txt.isprintable()

print(x)

20. Python String isspace() Method


Check if all the characters in the text are whitespaces:
txt = " "

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)

22. Python String isupper() Method


Check if all the characters in the text are in upper case:
txt = "THIS IS NOW!"

x = txt.isupper()

print(x)

23. Python String join() Method


Join all items in a tuple into a string, using a hash character as separator:
myTuple = ("John", "Peter", "Vicky")

x = "#".join(myTuple)

print(x)

24. Python String ljust() Method


Return a 20 characters long, left justified version of the word "banana":
txt = "banana"

x = txt.ljust(20)

print(x, "is my favorite fruit.")


25. Python String lower() Method
Lower case the string:
txt = "Hello my FRIENDS"

x = txt.lower()
26. Python String lstrip() Method
Remove spaces to the left of the string:
txt = " banana "

x = txt.lstrip()

print("of all fruits", x, "is my favorite")


print(x)

27. Python String maketrans() Method


Create a mapping table, and use it in the translate() method to replace any "S" characters with
a "P" character:
txt = "Hello Sam!"
mytable = str.maketrans("S", "P")
print(txt.translate(mytable))

28. Python String partition() Method


Search for the word "bananas", and return a tuple with three elements:
1 - everything before the "match"
2 - the "match"
3 - everything after the "match"
txt = "I could eat bananas all day"

x = txt.partition("bananas")

print(x)

29. Python String replace() Method


Replace the word "bananas":
txt = "I like bananas"

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)

31. Python String rindex() Method


Where in the text is the last occurrence of the string "casa"?:
txt = "Mi casa, su casa."

x = txt.rindex("casa")

print(x)

32. Python String rjust() Method


Return a 20 characters long, right justified version of the word "banana":
txt = "banana"

x = txt.rjust(20)

print(x, "is my favorite fruit.")

33. Python String rpartition() Method


Search for the last occurrence of the word "bananas", and return a tuple with three elements:
1 - everything before the "match"
2 - the "match"
3 - everything after the "match"
txt = "I could eat bananas all day, bananas are my favorite fruit"

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)

35. Python String rstrip() Method


Remove any white spaces at the end of the string:
txt = " banana "

x = txt.rstrip()

print("of all fruits", x, "is my favorite")

36. Python String split() Method


Split a string into a list where each word is a list item:
txt = "welcome to the jungle"

x = txt.split()

print(x)

37. Python String splitlines() Method


Split a string into a list where each line is a list item:
txt = "Thank you for the music\nWelcome to the jungle"

x = txt.splitlines()

print(x)

38. Python String startswith() Method


Check if the string starts with "Hello":
txt = "Hello, welcome to my world."
x = txt.startswith("Hello")
print(x)
39. Python String strip() Method
Remove spaces at the beginning and at the end of the string:
txt = " banana "

x = txt.strip()

print("of all fruits", x, "is my favorite")

40. Python String swapcase() Method


Make the lower case letters upper case and the upper case letters lower case:
txt = "Hello My Name Is PETER"

x = txt.swapcase()

print(x)

41. Python String title() Method


Make the first letter in each word upper case:
txt = "Welcome to my world"

x = txt.title()

print(x)

42. Python String translate() Method


Replace any "S" characters with a "P" character:
#use a dictionary with ascii codes to replace 83 (S) with 80 (P):
mydict = {83: 80}
txt = "Hello Sam!"
print(txt.translate(mydict))

43. Python String upper() Method


Upper case the string:
txt = "Hello my friends"
x = txt.upper()
print(x)
44. Python String zfill() Method
Fill the string with zeros until it is 10 characters long:
txt = "50"

x = txt.zfill(10)

print(x)

You might also like