Strings in Python Complete
Strings in Python Complete
● ord() function will return numeric values for Unicode characters as well.
chr()
● When you are stepping backward, if the first and second indices are omitted, the defaults are
reversed.
Modifying strings. But can you?
● No you can’t. But you already knew this. Hopefully.
● Strings are immutable.
● But you can easily accomplish what you want by generating a copy of the original string that has
the desired change in place.
Built-in String Methods
Case Conversion
“
Methods in this group perform case
conversion on the target string
1. s.capitalize()
➔ Capitalizes the target string.
➔ Returns a copy of s with the first character converted to
This is a slide title
uppercase and all other characters converted to lowercase.
➔ Non-alphabetic characters are unchanged.
2. s.lower()
➔ Returns a copy of s with all alphabetic characters converted to lowercase.
3. s.upper()
➔ Returns a copy of s with all alphabetic characters converted to uppercase.
4. s.swapcase()
➔ Returns a copy of s with uppercase alphabetic characters converted to lowercase and vice
versa.
5. s.title()
➔ Returns a copy of s in which the first letter of each word is converted to uppercase and
remaining letters are lowercase.
➔ It does not attempt to distinguish between important and unimportant words, and it does
not handle apostrophes, possessives, or acronyms gracefully.
Find
“ Big
concept
Methods in this group provide various
means of searching the target string
for a specified substring
1. s.count(<sub>[, <start>[, <end>]])
➔ Returns the number of non-overlapping occurrences of substring <sub> in s.
2. s.isalpha()
➔ Returns True if s is nonempty and all its characters are alphabetic, and False otherwise.
3. s.isdigit()
➔ Returns True if s is nonempty and all its characters are numeric digits, and False
4. s.islower()
➔ Returns True if s is nonempty and all the alphabetic characters it contains are lowercase,
and False otherwise.
➔ Non-alphabetic characters are ignored.
5. s.isupper()
➔ Returns True if s is nonempty and all the alphabetic characters it contains are uppercase,
6. s.isspace()
➔ Returns True if s is nonempty and all characters are whitespace characters, and False
otherwise.
➔ The most common whitespace characters are space ' ', tab '\t', and newline '\n'.
➔ However, there are a few other ASCII and Unicode characters that qualify as whitespace.
7. s.istitle()
➔ Returns True if s is nonempty, the first alphabetic character of each word is uppercase,
2. s.lstrip([<chars>])
➔ Returns a copy of s with any whitespace characters removed from the left end.
➔ If the optional <chars> argument is specified, it is a string that specifies the set of
characters to be removed.
3. s.rstrip([<chars>])
➔ Returns a copy of s with any whitespace characters removed from the right end.
In two or three columns
➔ If the optional <chars> argument is specified, it is a string that specifies the set of
characters to be removed.
4. s.strip([<chars>])
➔ Equivalent to invoking s.lstrip() and s.rstrip() in succession.
➔ Without the <chars> argument, it removes leading and trailing whitespace.
➔ The optional <chars> argument specifies the set of characters to be removed.
Conversion between
“ Strings & Lists
➔ If <sep> is specified with a value of None, the string is split delimited by whitespace, just
as though <sep> had not been specified at all.
➔ When <sep> is explicitly given as a delimiter, consecutive delimiters in s are assumed to
delimit empty strings, which will be returned.
a) True
b) False
Ans: (a)
2. What is the slice expression that gives
every third character of string s, starting with
the last character and proceeding backward to
the first?
Ans: s[::-3]
3. Suppose s = ‘foobar’ .
c) s[::-1][-1] + s[len(s)-1]
d) s[::5]
e) s[0] + s[-1]
Ans: (a)
4. Which of the following are true?
a) s[::-1][::-1] is s
b) s[:] == s
c) s[:] is s
d) s[::-1][::-1] == s
Ans: (b),(c),(d)
5. What is the output from this print()
function call?
a) 3 1 0
b) 3 1 2
c) 3 1 1
d) 3 2 1
Ans: (b)
6. Suppose s = ‘foo-bar-baz’.
c) s.strip('-')
d) '-'.join(s.partition('-'))
Ans: (a),(b),(c)
7. What is the result of this statement?
print(ord('foo')) a) 324
b) 102
d) Exception
Ans: (d)
Fin.