Professional Documents
Culture Documents
Strings Part 2 - String Methods
Strings Part 2 - String Methods
1
Strings
●Built-in functions to Work with Python
Various built-in functions that work with sequence work with strings as
●
well.
●Some of the commonly used ones are enumerate() and len().
●The enumerate() function returns an enumerate object. It contains the index
and value of all the items in the string as pairs. This can be useful for
iteration.
●Similarly, len() returns the length (number of characters) of the string.
24-12-2021 2
Strings
24-12-2021 3
Strings
●Advantages of using Enumerate
●Here, are pros/benefits of using Enumerate in Python:
●Enumerate allows you to loop through a list, tuple, dictionary, string, and
gives the values along with the index.
●To get index value using for-loop, you can make use of list.index(n).
However, list.index(n) is very expensive as it will traverse the for-loop
twice. Enumerate is very helpful in such a case as it gives the index and
items at one go.
24-12-2021 4
Strings
●Python String Formatting
●Escape Sequence
●If we want to print a text like He said, "What's there?", we can neither use
single quotes nor double quotes. This will result in a SyntaxError as the text
itself contains both single and double quotes.
24-12-2021 5
Strings
●Python String Formatting
●One way to get around this problem is to use triple quotes. Alternatively, we
can use escape sequences.
●An escape sequence starts with a backslash and is interpreted differently.
24-12-2021 6
Strings
●Python String Formatting
●Other escape sequences
24-12-2021 7
Strings
●Python String Formatting
●Raw String to ignore escape sequence
●Sometimes we may wish to ignore the escape sequences inside a string. To
do this we can place r or R in front of the string.
24-12-2021 8
Strings
●The string Objects support many methods.
●We group these methods into various classes:
●Alignment Methods
●IsX() Methods
●Case Sensitive Methods
●Searching Methods
●General Methods
●Character Modification Methods
24-12-2021 9
Strings
IsX Methods
●
Methods:
●
isupper() istitle()
24-12-2021 10
Strings
●isalnum()
24-12-2021 11
Strings
●isalpha()
24-12-2021 12
Strings
●isdecimal()
24-12-2021 13
Strings
●isdigit()
24-12-2021 14
Strings
●isidentifier()
24-12-2021 15
Strings
●islower()
24-12-2021 16
Strings
●isnumeric()
24-12-2021 17
Strings
●isprintable()
24-12-2021 18
Strings
●isspace()
24-12-2021 19
Strings
●istitle()
24-12-2021 20
Strings
●isupper()
24-12-2021 21
Strings
●Alignment Methods- ljust, rjust, center
24-12-2021 22
Strings
●Alignment Methods- ljust, rjust, center
24-12-2021 23
Strings
●Alignment Methods- ljust, rjust, center
24-12-2021 24
Strings
●Case Sensitive methods
●upper()
24-12-2021 25
Strings
●Case Sensitive methods
●lower()
24-12-2021 26
Strings
●Case Sensitive methods
●title()
24-12-2021 27
Strings
●Case Sensitive methods
●capitalise()
24-12-2021 28
Strings
●Case Sensitive methods
●casefold()
24-12-2021 29
Strings
●Case Sensitive methods
●casefold()
24-12-2021 30
Strings
●Case Sensitive methods
●swapcase()
24-12-2021 31
Strings
●General Methods
●Format Method
# default arguments
print("Hello {}, your balance is {}.".format("Adam", 230.2346))
# positional arguments
print("Hello {0}, your balance is {1}.".format("Adam", 230.2346))
# keyword arguments
print("Hello {name}, your balance is {blc}.".format(name="Adam", blc=230.2346))
# mixed arguments
print("Hello {0}, your balance is {blc}.".format("Adam", blc=230.2346))
24-12-2021 32
Strings
●General Methods
●count()
24-12-2021 33
Strings
●General Methods
●endswith()
24-12-2021 34
Strings
●General Methods
●startswith()
24-12-2021 35
Strings
●General Methods
●find()
24-12-2021 36
Strings
●General Methods
●rfind()
24-12-2021 37
Strings
●General Methods
●Index() and rindex()
Just like find() they return the index of the first occurrence of a substring.
●
But index() returns an “Exception” when a substring is not found while find
method returns -1.
24-12-2021 38
Strings
●Character modification Methods
●Split()
24-12-2021 39
Strings
●Character modification Methods
●Splitlines()
24-12-2021 40
Strings
●Character modification Methods
●join()
24-12-2021 41
Strings
●Character modification Methods
●Strip()
●Rstrip()
●lstrip()
24-12-2021 42
Strings
●Character modification Methods
●replace()
24-12-2021 43
Strings
●Character modification Methods
●partition()
24-12-2021 44
Strings
●Character modification Methods
●rpartition()
24-12-2021 45
Strings
●Character modification Methods
●zfill()
24-12-2021 46