Chapter 4 Strings¶
Keynote¶
4.1 What are Strings?¶
-
Python string is a collection of Unicode characters.
-
Python strings can be enclosed in single, double or triple quotes.
Handling Special Characters in Strings
If there are characters like ', " or \ within a string, they can be retained in two ways:
- Escape Sequence: Escape them by preceding them with a
\(backslash). - Raw String: Prepend the string with a
rindicating that it is a raw string.
# (a) Using Backslash to escape quotes
msg_escape = 'He said, \'Python With Joe.\''
print(msg_escape)
# Output: He said, 'Python With Joe.'
# (b) Using Raw String (r prefix)
# Useful when you have many backslashes (e.g., file paths or regex)
path = r"C:\Users\Name\Documents"
print(path)
# Output: C:\Users\Name\Documents
Creating Multiline Strings
Multiline strings can be created in 3 ways:
- Using Backslash (
\): All but the last line ends with a backslash. - Triple Quotes: Enclosed within
"""..."""or'''...'''. - Parentheses: Using implicit string concatenation within brackets
().
# Method 1: Using Backslash (\)
# Note: Ensure there is no space after the backslash
msg1 = 'one msg ' \
'another msg'
# Method 2: Triple Quotes
# This preserves the newline characters inside the string
msg2 = """one msg
another msg"""
# Method 3: Using Parentheses
# Often preferred for long strings to keep code clean
msg3 = ('one msg '
'another msg')
4.2 Accessing String Elements¶
- String elements can be accessed using an index value, starting with 0.
- Negative index value is allowed. The last character is considered to be at index -1.
4.2.1 Visualizing Indices¶
The relationship between positive and negative indices is shown below using the string "PYTHON" as an example:
| Character | P | Y | T | H | O | N |
|---|---|---|---|---|---|---|
| Positive Index | 0 | 1 | 2 | 3 | 4 | 5 |
| Negative Index | -6 | -5 | -4 | -3 | -2 | -1 |
s = "PYTHON"
# Accessing first character
print(s[0]) # Output: P
print(s[-6]) # Output: P
# Accessing last character
print(s[5]) # Output: N
print(s[-1]) # Output: N
4.2.2 String Slicing¶
A sub-string can be sliced out of a string using the syntax [start : end].
Common Slicing Patterns
| Syntax | Description |
|---|---|
s[start : end] |
Extract from start to end - 1. |
s[start :] |
Extract from start to the end of string. |
s[: end] |
Extract from beginning to end - 1. |
s[-start :] |
Extract from -start (included) to end. |
s[: -end] |
Extract from beginning to -end - 1. |
Handling Out-of-Bounds Indices
One important feature of slicing is how it handles indices that are larger than the string length.
Slicing vs. Direct Indexing
- Direct Indexing (
msg[100]): Raises an Error if the index is too large. - Slicing (
msg[3:100]): Handled elegantly. It simply returns characters up to the end of the string without crashing.
msg = 'Rafting'
# Slicing with a large index (Safe)
print(msg[3:100])
# Output: 'ting' (extracts from 't' up to end)
# Direct indexing with a large index (Unsafe)
# print(msg[100])
# Error: IndexError: string index out of range
4.3 String Properties¶
Python strings are immutableβthey cannot be changed once created.
- Immutability: Python strings are immutableβthey cannot be changed in place.
- Concatenation: Strings can be joined using
+. - Replication: Strings can be repeated using
*. - Membership: Check if a substring exists using
in.
Basic String Operations
Python provides intuitive operators to manipulate strings easily.
Concatenation (+)
Strings can be joined together (concatenated) using the + operator.
msg1 = "Hello "
msg2 = "World"
msg3 = msg1 + msg2
print(msg3)
# Output: Hello World
Replication (*)
Strings can be repeated (replicated) using the * operator. This is very useful for formatting output, such as creating separator lines.
Syntax Note
To replicate a string, you must use the asterisk *, not a comma.
print('-', 50)will simply print the character and the number (e.g.,- 50).print('-' * 50)will print the character 50 times.
Membership Testing (in)
Membership testing checks if a substring exists within a string. It returns True if the substring is found, otherwise False.
s = "Hello World"
print('World' in s) # Output: True
print('Python' in s) # Output: False
String Length (len())
The len() function returns the number of characters in a string.
String Indexing
String indexing allows you to access individual characters in a string using their position (index).
String Slicing
String slicing allows you to extract a portion of a string using a range of indices.
s = "Hello World"
print(s[0:5]) # Output: Hello
print(s[-6:-1]) # Output: World
Modern String Formatting (f-strings)
Up-to-Date Feature (Python 3.6+)
- The traditional way to combine strings and variables is using
+(Concatenation) or.format(). However,f-strings(Formatted String Literals) are the modern, preferred way because they are more readable and faster.
4.5 Built-in Functions & Methods¶
Built-in Functions:
len(s): Returns the length of the string.min(s): Returns the character with the minimum Unicode value.max(s): Returns the character with the maximum Unicode value.
Content Test Methods
isalpha(): Checks if all characters are alphabets.isdigit(): Checks if all characters are digits.isalnum(): Checks if all characters are alphanumeric.islower()/isupper(): Checks case.startswith()/endswith(): Checks prefix/suffix.
Search and Replace
find(sub): Returns index of substring (returns -1 if not found).replace(old, new): Replaces occurrences of a substring.
Trimming & Splitting
1. lstrip(), rstrip(), strip(): Removes whitespace from ends.
2. split(sep): Splits string into a list.
3. join(iterable): Joins elements into a string.
Modern Python: removing prefix/suffix (Python 3.9+)
- Using
strip()to remove specific words can be risky (it removes character sets). Modern Python provides safer methods:
filename = "report.pdf"
print(filename.removesuffix(".pdf")) # Output: report
print(filename.removeprefix("report")) # Output: .pdf
4.6 String Conversions & Comparison¶
- Case Conversion: upper(), lower(), capitalize(), title(), swapcase().
- Type Conversion: str(), int(), float(), complex().
- ASCII/Unicode:
ord('A'): Returns integer 65.chr(65): Returns character 'A'.
- Comparison: Done lexicographically (dictionary order).
s1 > s2(Evaluates based on Unicode values).
Solved Problems¶
Problem 4.1¶
Question: Demonstrate how to create simple and multi-line strings and whether a
string can change after creation. Also show the usage of built-in
functions len(), min(), max() on a string.
Program
# simple strings
msg1 = 'Hoopla'
print(msg1)
# strings with special characters
msg2 = 'He said, \'Python With Joe\'.'
file1 = 'C:\\temp\\newfile'
file2 = r'C:\temp\newfile' # raw string - prepend r
print(msg2)
print(file1)
print(file2)
# multiline strings
# whitespace at beginning of second line becomes part of string
msg3 = 'What is this life if full of care...\
We have no time to stand and stare'
# enter at the end of first line becomes part of string
msg4 = """What is this life if full of care...
We have no time to stand and stare"""
# strings are concatenated properly.( ) necessary
msg5 = ('What is this life if full of care...'
'We have no time to stand and stare')
print(msg3)
print(msg4)
print(msg5)
# string replication during printing
msg6 = 'MacLearn!!'
print(msg1 * 3)
# immutability of strings
# Utopia cannot change, msg7 can
msg7 = 'Utopia'
msg8 = 'Today!!!'
msg7 = msg7 + msg8 # concatenation using +
print(msg7)
# use of built-in functions on strings
print(len('Hoopla'))
print(min('Hoopla'))
print(max('Hoopla'))
Output
Hoopla
He said, 'Python With Joe'.
C:\temp\newfile
C:\temp\newfile
What is this life if full of care... We have no time to stand and stare
What is this life if full of care...
We have no time to stand and stare
What is this life if full of care...We have no time to stand and stare
HooplaHooplaHoopla
UtopiaToday!!!
6
H
p
Tips
- Special characters can be retained in a string by either escaping them or by marking the string as a raw string.
- Strings cannot change, but the variables that store them can.
len( )returns the number of characters present in string.min( )andmax( )return the character with minimum and maximum Unicode value from the string.
Problem 4.2¶
Question: For a given string 'Bamboozled', write a program to obtain the following output:
B a
e d
e d
mboozled
mboozled
mboozled
Bamboo
Bamboo
Bamboo
Bamboo
delzoobmaB
Bamboozled
Bmoze
Bbzd
Boe
BamboozledHype!
BambooMonger!
Program
s = 'Bamboozled'
# extract B a
print(s[0], s[1])
print(s[-10], s[-9])
# extract e d
print(s[8], s[9])
print(s[-2], s[-1])
# extract mboozled
print(s[2:10])
print(s[2:])
print(s[-8:])
# extract Bamboo
print(s[0:6])
print(s[:6])
print(s[-10:-4])
print(s[:-4])
# reverse Bamboozled
print(s[::-1])
print(s[0:10:1])
print(s[0:10:2])
print(s[0:10:3])
print(s[0:10:4])
s = s + 'Hype!'
print(s)
s = s[:6] + 'Monger' + s[-1]
print(s)
Tips
- Special characters can be retained in a string by either escaping them or by marking the string as a raw string.
s[4:8]is same ass[4:8:1], where 1 is the default.s[4:8:2]returns a character, then move forward 2 positions, etc.
Problem 4.3¶
Question: For the following strings find out which are having only alphabets, which are numeric, which are alphanumeric, which are lowercase, which are uppercase. Also find out whether 'And Quiet Flows The Don' begins with 'And' or ends with 'And' : 'NitiAayog' 'And Quiet Flows The Don' '1234567890' 'Make $1000 a day'
Program
s1 = 'NitiAayog'
s2 = 'And Quiet Flows The Don'
s3 = '1234567890'
s4 = 'Make $1000 a day'
print('s1 = ', s1)
print('s2 = ', s2)
print('s3 = ', s3)
print('s4 = ', s4)
# Content test functions
print('check isalpha on s1, s2')
print(s1.isalpha( ))
print(s2.isalpha( ))
print('check isdigit on s3, s4')
print(s3.isdigit( ))
print(s4.isdigit( ))
print('check isalnum on s1, s2, s3, s4')
print(s1.isalnum( ))
print(s2.isalnum( ))
print(s3.isalnum( ))
print(s4.isalnum( ))
print('check islower on s1, s2')
print(s1.islower( ))
print(s2.islower( ))
print('check isupper on s1, s2')
print(s1.isupper( ))
print(s2.isupper( ))
print('check startswith and endswith on s2')
print(s2.startswith('And'))
print(s2.endswith('And'))
Output
s1 = NitiAayog
s2 = And Quiet Flows The Don
s3 = 1234567890
s4 = Make $1000 a day
check isalpha on s1, s2
True
False
check isdigit on s3, s4
True
False
check isalnum on s1, s2, s3, s4
True
False
True
False
check islower on s1, s2
False
False
check isupper on s1, s2
False
False
check startswith and endswith on s2
True
False
Problem 4.4¶
Question: Given the following string: 'Bring It On' ' Flanked by spaces on either side ' 'C:\Users\Joseph\Documents' write a program to produce the following output using appropriate string functions. BRING IT ON bring it on Bring it on Bring It On bRING iT oN 6 9 Bring Him On Flanked by spaces on either side Flanked by spaces on either side ['C:', 'Users', 'Joseph', 'Documents'] ('C:', '\', 'Users\Joseph\Documents')
Program
s1 = 'Bring It On'
# Conversions
print(s1.upper( ))
print(s1.lower( ))
print(s1.capitalize( ))
print(s1.title( ))
print(s1.swapcase( ))
# search and replace
print(s1.find('I'))
print(s1.find('On'))
print(s1.replace('It', 'Him'))
# trimming
s2 = ' Flanked by spaces on either side '
print(s2.lstrip( ))
print(s2.rstrip( ))
# splitting
s3 = 'C:\\Users\\Joseph\\Documents'
print(s3.split('\\'))
print(s3.partition('\\'))
Problem 4.5¶
Question: Find all occurrences of 'T' in the string 'The Terrible Tiger Tore The Towel'. Replace all occurrences of 'T' with 't'.
Program
s = 'The Terrible Tiger Tore The Towel'
pos = s.find('T', 0)
print(pos, s[pos])
pos = s.find('T', pos + 1)
print(pos, s[pos])
pos = s.find('T', pos + 1)
print(pos, s[pos])
pos = s.find('T', pos + 1)
print(pos, s[pos])
pos = s.find('T', pos + 1)
print(pos, s[pos])
pos = s.find('T', pos + 1)
print(pos, s[pos])
pos = s.find('T', pos + 1)
print(pos)
c = s.count('T')
s = s.replace('T', 't', c)
print(s)
Tips
- First call to search( ) returns the position where first 'T' is found. To search subsequent 'T' search is started from pos + 1.
- When 'T' is not found search( ) returns -1.
- count( ) returns the number of occurrences of 'T' in the string.
- Third parameter in the call to replace( ) indicates number of replacements to be done.
Exercise¶
[A] Answer the following questions:
a. Write a program that generates the following output from the string 'Shenanigan'.
b. Write a program to convert the following string 'Visit ykanetkar.com for online courses in programming' into 'Visit Ykanetkar.com For Online Courses In Programming'
c. Write a program to convert the following string 'Light travels faster than sound. This is why some people appear bright until you hear them speak.' into 'LIGHT travels faster than SOUND. This is why some people appear bright until you hear them speak.'
d. What will be the output of the following program? s = 'HumptyDumpty' print('s = ', s) print(s.isalpha( )) print(s.isdigit( )) print(s.isalnum( )) print(s.islower( )) print(s.isupper( )) print(s.startswith('Hump')) print(s.endswith('Dump'))
e. What is the purpose of a raw string?
f. If we wish to work with an individual word in the following string, how will you separate them out: 'The difference between stupidity and genius is that genius has its limits'
g. Mention two ways to store a string: He said, "Python With Joe".
h. What will be the output of following code snippet?
i. What will be the output of the following code snippet?j. Strings in Python are iterable, sliceable and immutable. (True/False)
k. How will you extract ' TraPoete' from the string 'ThreadProperties'?
l. How will you eliminate spaces on either side of the string ' Flanked by spaces on either side '?
m. What will be the output of the following code snippet?
n. What will get stored in ch in the following code snippet:
[B] Match the following pairs Assumingmsg = 'Keep yourself warm':
| Option A | Description | Option B | Match |
|---|---|---|---|
a. msg.partition(' ') |
Splits at the first space into a 3-tuple. | ('Keep', ' ', 'yourself warm') |
8 |
b. msg.split(' ') |
Splits by space into a list of substrings. | ['Keep', 'yourself', 'warm'] |
7 |
c. msg.startswith('Keep') |
Checks if string starts with ""Keep"". | True | 5 |
d. msg.endswith('Keep') |
Checks if string ends with ""Keep"". | False | 6 |
e. msg.swapcase() |
Swaps uppercase to lowercase and vice versa. | kEEP YOURSELF WARM | 2 |
f. msg.capitalize() |
Capitalizes first letter, lowercases the rest. | Keep yourself warm | 3 |
g. msg.count('e') |
Counts occurrences of 'e'. | 3 | 4 |
h. len(msg) |
Returns the total length of the string. | 18 | 1 |
i. msg[0] |
Accesses the first character. | K | 11 |
j. msg[-1] |
Accesses the last character. | m | 13 |
k. msg[1:1:1] |
Slicing with same start/end yields empty. | empty string | 12 |
l. msg[-1:3] |
Slicing with positive step from right to left invalid. | empty string | 15 |
m. msg[:-3] |
Slices from start up to the last 3 characters. | Keep yourself w | 9 |
n. msg[-3:] |
Slices the last 3 characters. | arm | 14 |
o. msg[0:-2] |
Slices from start up to the last 2 characters. | Keep yourself wa | 10 |