0% found this document useful (0 votes)
25 views

Strings Part 2 - String Methods

This document discusses various string methods in Python. It describes built-in functions like len() and enumerate() that can be used on strings. It then covers string formatting, escape sequences, alignment methods like ljust and rjust, case conversion methods, searching methods like find and count, and character modification methods like split, strip and replace.
Copyright
© © All Rights Reserved
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Strings Part 2 - String Methods

This document discusses various string methods in Python. It describes built-in functions like len() and enumerate() that can be used on strings. It then covers string formatting, escape sequences, alignment methods like ljust and rjust, case conversion methods, searching methods like find and count, and character modification methods like split, strip and replace.
Copyright
© © All Rights Reserved
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
You are on page 1/ 46

Python Programming

C. Ravi Kishore Reddy


Assistant Professor
Department of CSE, VFSTR

15/06/2022 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.

15/06/2022 2
Strings

15/06/2022 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.

15/06/2022 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.

15/06/2022 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.

15/06/2022 6
Strings
● Python String Formatting
● Other escape sequences

15/06/2022 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.

15/06/2022 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

15/06/2022 9
Strings
● IsX Methods
● These methods return a boolean value based on condition on X
● Methods:
isaplha() isalnum() isdecimal()

isdigit() isidentifier() islower()

isnumeric() isprintable() isspace()

isupper() istitle()

15/06/2022 10
Strings
● isalnum()

15/06/2022 11
Strings
● isalpha()

15/06/2022 12
Strings
● isdecimal()

15/06/2022 13
Strings
● isdigit()

15/06/2022 14
Strings
● isidentifier()

15/06/2022 15
Strings
● islower()

15/06/2022 16
Strings
● isnumeric()

15/06/2022 17
Strings
● isprintable()

15/06/2022 18
Strings
● isspace()

15/06/2022 19
Strings
● istitle()

15/06/2022 20
Strings
● isupper()

15/06/2022 21
Strings
● Alignment Methods- ljust, rjust, center

15/06/2022 22
Strings
● Alignment Methods- ljust, rjust, center

15/06/2022 23
Strings
● Alignment Methods- ljust, rjust, center

15/06/2022 24
Strings
● Case Sensitive methods
● upper()

15/06/2022 25
Strings
● Case Sensitive methods
● lower()

15/06/2022 26
Strings
● Case Sensitive methods
● title()

15/06/2022 27
Strings
● Case Sensitive methods
● capitalise()

15/06/2022 28
Strings
● Case Sensitive methods
● casefold()

15/06/2022 29
Strings
● Case Sensitive methods
● casefold()

15/06/2022 30
Strings
● Case Sensitive methods
● swapcase()

15/06/2022 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))
15/06/2022 32
Strings
● General Methods
● count()

15/06/2022 33
Strings
● General Methods
● endswith()

15/06/2022 34
Strings
● General Methods
● startswith()

15/06/2022 35
Strings
● General Methods
● find()

15/06/2022 36
Strings
● General Methods
● rfind()

15/06/2022 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.

15/06/2022 38
Strings
● Character modification Methods
● Split()

15/06/2022 39
Strings
● Character modification Methods
● Splitlines()

15/06/2022 40
Strings
● Character modification Methods
● join()

15/06/2022 41
Strings
● Character modification Methods
● Strip()
● Rstrip()
● lstrip()

15/06/2022 42
Strings
● Character modification Methods
● replace()

15/06/2022 43
Strings
● Character modification Methods
● partition()

15/06/2022 44
Strings
● Character modification Methods
● rpartition()

15/06/2022 45
Strings
● Character modification Methods
● zfill()

15/06/2022 46

You might also like