Chapter 8 Strings
Chapter 8 Strings
Chapter 8: Strings
Introduction to Strings
• Definition: A string is a sequence of characters.
• Strings are immutable in Python, meaning once created, they cannot be changed.
Explanation: single_line is a simple string using single quotes, while multi_line is a multi-line
string using triple quotes.
Explanation: This program accesses characters in str1 using positive and negative indices.
1
Chapter 8 Strings
[ ]: str1 = 'Hello'
str2 = 'World!'
result = str1 + ' ' + str2
print(result) # Output: 'Hello World!'
Explanation: This program joins str1 and str2 with a space in between using the + operator.
8.3.2 Repetition
• Use * to repeat a string multiple times.
Example:
[ ]: str1 = 'Hello'
print(str1 * 2) # Output: 'HelloHello'
print(str1 * 5) # Output: 'HelloHelloHelloHelloHello'
Explanation: This program repeats the string str1 2 and 5 times using the * operator.
8.3.3 Membership
• Use in and not in to check for the presence of a substring.
Example:
Explanation: This program checks if ‘W’ is in str1 and if ‘My’ is not in str1 using the in and
not in operators.
8.3.4 Slicing
• Extract substrings using str1[start:end].
– start is inclusive, end is exclusive.
Example:
Explanation: This program slices str1 to extract substrings using different start, end, and step
values.
Explanation: This program uses a for loop to iterate over each character in str1 and print them
with a space in between.
[ ]:
Explanation: This program uses a while loop to iterate over each character in str1 and print them
with a space in between.
[ ]: print(len('Hello'))
Explanation: This method capitalizes the first letter of each word in the string ‘hello world’.
Explanation: This method converts all characters in the string ‘HELLO’ to lowercase.
Explanation: This method converts all characters in the string ‘hello’ to uppercase.
[ ]: print('hello'.count('l')) # Output: 2
Explanation: This method counts the number of times ‘l’ appears in the string ‘hello’.
[ ]: print('hello'.find('e')) # Output: 1
Explanation: This method returns the index of the first occurrence of ‘e’ in the string ‘hello’.
index(): Finds the first occurrence of a substring (raises error if not found). Example:
[ ]: print('hello'.index('e')) # Output: 1
Explanation: This method returns the index of the first occurrence of ‘e’ in the string ‘hello’. If ‘e’
is not found, it raises a ValueError.
Explanation: This method returns True if the string ‘hello’ ends with ‘o’, otherwise it returns
False.
Explanation: This method returns True if the string ‘hello’ starts with ‘h’, otherwise it returns
False.
Explanation: This method returns True if all characters in the string ‘hello123’ are alphanumeric,
otherwise it returns False.
Explanation: This method returns True if all characters in the string ‘hello’ are lowercase, otherwise
it returns False.
Explanation: This method returns True if all characters in the string ‘HELLO’ are uppercase,
otherwise it returns False.
Explanation: This method returns True if all characters in the string ’ ’ are whitespace, otherwise
it returns False.
Explanation: This method returns True if the string ‘Hello World’ is title-cased, otherwise it returns
False.
Explanation: This method removes leading whitespace from the string ’ hello’.
Explanation: This method removes trailing whitespace from the string ‘hello’.
Explanation: This method removes both leading and trailing whitespace from the string ’ hello ’.
Explanation: This method replaces all occurrences of ‘l’ with ‘x’ in the string ‘hello’.
Explanation: This method joins the characters of the string ‘hello’ with ‘-’ as a separator.
Explanation: This method splits the string ‘hello world’ at the first occurrence of ’ ’ into a tuple
containing the part before the separator, the separator itself, and the part after the separator.
Explanation: This method splits the string ‘hello world’ at each occurrence of ’ ’ into a list of
substrings.
Exercises
1. Write a Python program to find the length of a string.
[ ]:
2. Write a Python program to count the number of times a character appears in a string.
[ ]:
[ ]:
[ ]:
4. Write a Python program to convert a string to uppercase without using the upper() method.
[ ]:
[ ]:
[ ]:
[ ]:
[ ]: