Mastering Python : Day 7
Here are examples of each of the string methods in Python:
1. capitalize()
Converts the first character to upper case
s = "hello world"
print(s.capitalize()) # Output: "Hello world"
2. casefold()
Converts string into lower case
s = "HELLO WORLD"
print(s.casefold()) # Output: "hello world"
3. center()
Returns a centered string
s = "hello"
print(s.center(10)) # Output: " hello "
4. count()
Returns the number of times a specified value occurs in a string
s = "hello world"
print(s.count("l")) # Output: 3
5. encode()
Returns an encoded version of the string
s = "hello world"
print(s.encode("utf-8")) # Output: b'hello world'
6. endswith()
Returns true if the string ends with the specified value
s = "hello world"
print(s.endswith("world")) # Output: True
7. expandtabs()
Sets the tab size of the string
s = "hello\tworld"
print(s.expandtabs(4)) # Output: "hello world"
8. find()
Searches the string for a specified value and returns the position of where it was found
s = "hello world"
print(s.find("world")) # Output: 6
9. format()
Formats specified values in a string
s = "My name is {} and I am {} years old."
print(s.format("John", 30)) # Output: "My name is John and I am 30 years old."
10. format_map()
Formats specified values in a string
s = "My name is {name} and I am {age} years old."
d = {"name": "John", "age": 30}
print(s.format_map(d)) # Output: "My name is John and I am 30 years old."
11. index()
Searches the string for a specified value and returns the position of where it was found
s = "hello world"
print(s.index("world")) # Output: 6
12. isalnum()
Returns True if all characters in the string are alphanumeric
s = "hello123"
print(s.isalnum()) # Output: True
13. isalpha()
Returns True if all characters in the string are in the alphabet
s = "hello"
print(s.isalpha()) # Output: True
14. isascii()
Returns True if all characters in the string are ascii characters
s = "hello"
print(s.isascii()) # Output: True
15. isdecimal()
Returns True if all characters in the string are decimals
s = "123"
print(s.isdecimal()) # Output: True
16. isdigit()
Returns True if all characters in the string are digits
s = "123"
print(s.isdigit()) # Output: True
17. isidentifier()
Returns True if the string is an identifier
s = "hello"
print(s.isidentifier()) # Output: True
18. islower()
Returns True if all characters in the string are lower case
s = "hello"
print(s.islower()) # Output: True
19. isnumeric()
Returns True if all characters in the string are numeric
s = "123"
print(s.isnumeric()) # Output: True
20. isprintable()
Returns True if all characters in the string are printable
s = "hello"
print(s.isprintable()) # Output: True
21. isspace()
Returns True if all characters in the string are whitespaces
s = " "
print(s.isspace()) # Output: True
22. istitle()
Returns True if the string follows the rules of a title
s = "Hello World"
print(s.istitle()) # Output: True
23. isupper()
Returns True if all characters in the string are upper case
s = "HELLO"
print(s.isupper()) # Output: True
24. join()
Joins the elements of an iterable to the end of the string
s = "Hello"
words = ["world", "this", "is", "a", "test"]
print(" ".join(words)) # Output: "world this is a test"
print("-".join(words)) # Output: "world-this-is-a-test"
print(s + " " + " ".join(words)) # Output: "Hello world this is a test"
25. ljust()
Returns a left justified version of the string
s = "Hello"
print(s.ljust(10)) # Output: "Hello "
print(s.ljust(10, "*")) # Output: "Hello*****"
26. lower()
Converts a string into lower case
s = "HELLO"
print(s.lower()) # Output: "hello"
27. lstrip()
Returns a left trim version of the string
s = " Hello "
print(s.lstrip()) # Output: "Hello "
print(s.lstrip("H")) # Output: "ello "
28. maketrans()
Returns a translation table to be used in translations
s = "Hello world"
table = str.maketrans("H", "J")
print(s.translate(table)) # Output: "Jello world"
29. partition()
Returns a tuple where the string is parted into three parts
s = "Hello world"
print(s.partition(" ")) # Output: ("Hello", " ", "world")
30. replace()
Returns a string where a specified value is replaced with a specified value
s = "Hello world"
print(s.replace("world", "Python")) # Output: "Hello Python"
31. rfind()
Searches the string for a specified value and returns the last position of where it was found
s = "Hello world, world"
print(s.rfind("world")) # Output: 13
32. rindex()
Searches the string for a specified value and returns the last position of where it was found
s = "Hello world, world"
print(s.rindex("world")) # Output: 13
33. rjust()
Returns a right justified version of the string
s = "Hello"
print(s.rjust(10)) # Output: " Hello"
print(s.rjust(10, "*")) # Output: "*****Hello"
34. rpartition()
Returns a tuple where the string is parted into three parts
s = "Hello world"
print(s.rpartition(" ")) # Output: ("Hello", " ", "world")
35. rsplit()
Splits the string at the specified separator, and returns a list
s = "Hello world, world"
print(s.rsplit(" ", 1)) # Output: ["Hello world,", "world"]
36. rstrip()
Returns a right trim version of the string
s = " Hello "
print(s.rstrip()) # Output: " Hello"
print(s.rstrip("H")) # Output: " ello"
37. split()
Splits the string at the specified separator, and returns a list
s = "Hello world, world"
print(s.split(" ")) # Output: ["Hello", "world,", "world"]
38. splitlines()
Splits the string at line breaks and returns a list
s = "Hello\nworld"
print(s.splitlines()) # Output: ["Hello", "world"]
39. startswith()
Returns true if the string starts with the specified value
s = "Hello world"
print(s.startswith("Hello")) # Output: True
40. strip()
Returns a trimmed version of the string
s = " Hello "
print(s.strip()) # Output: "Hello"
print(s.strip("H")) # Output: "ello"
41. swapcase()
Swaps cases, lower case becomes upper case and vice versa
s = "Hello world"
print(s.swapcase()) # Output: "hELLO WORLD"
42. title()
Converts the first character of each word to upper case
s = "hello world"
print(s.title()) # Output: "Hello World"
43. translate()
Returns a translated string
s = "Hello world"
table = str.mak