As you know, the stings are widely used to hold textual data. To perform any programming tasks in Python, a good understanding of string manipulation is necessary.
Exercise 1A: Create a string
Write a program to create a new string made of an input string’s first, middle, and last character.
Example:
Input
str1 = "James"
Output
Jms
Hint:
- String index always starts with 0
- Use string indexing to get the character present at the given index
- Get the index of the middle character by dividing string length by 2
Solution:
- Use string indexing to get the character present at the given index.
- Use
str1[0]to get the first character of a string and add it to theresultvariable - Next, get the middle character’s index by dividing string length by 2. \$
x = len(str1)/2\$. Usestr1[x]to get the middle character and add it to theresultvariable - Use
str1[len(str1)-1]to get the last character of a string and add it to theresultvariable - print
resultvariable to display new string
str1 = 'James'
print("Original String is", str1)
# Get first character
res = str1[0]
# Get string size
l = len(str1)
# Get middle index number
mi = int(l / 2)
# Get middle character and add it to result
res = res + str1[mi]
# Get last character and add it to result
res = res + str1[l - 1]
print("New String:", res)
Exercise 1B: Create a string made of the middle three characters
Write a program to create a new string made of the middle three characters of an input string.
Example
Input
str1 = "JhonDipPeta"
Output
Dip
Input
str2 = "JaSonAy"
Output
Son
Hint:
- First, get the middle index number by dividing string length by 2.
- Use string slicing to get the middle three characters starting from the middle index to the next two character
.
Solution:
- Get the middle character’s index using
x = len(str1) /2. - Use string slicing to get the middle three characters starting from the middle index to the next two character
str1[middle_index-1:middle_index+2]
def get_middle_three_chars(str1):
print("Original String is", str1)
# first get middle index number
mi = int(len(str1) / 2)
# use string slicing to get result characters
res = str1[mi - 1:mi + 2]
print("Middle three chars are:", res)
get_middle_three_chars("JhonDipPeta")
get_middle_three_chars("JaSonAy")
Exercise 2: Append new string in the middle of a given string
Given two strings, s1 and s2. Write a program to create a new string s3 by appending s2 in the middle of s1.
Example
Input
s1 = "Ault" s2 = "Kelly"
Output
AuKellylt
Hint:
- Use built-in function
len(s1)to get the string length. - Next, get the middle index number by dividing string length by 2.
Solution:
- First, get the middle index number of
s1by dividing s1’s length by 2 - Use string slicing to get the character from
s1starting from 0 to the middle index number and store it inx - concatenate
xands2.x = x + s2 - concatenate
xand remeaning character froms1 - print
x
def append_middle(s1, s2):
print("Original Strings are", s1, s2)
# middle index number of s1
mi = int(len(s1) / 2)
# get character from 0 to the middle index number from s1
x = s1[:mi:]
# concatenate s2 to it
x = x + s2
# append remaining character from s1
x = x + s1[mi:]
print("After appending new string in middle:", x)
append_middle("Ault", "Kelly")
Exercise 3: Create a new string made of the first, middle, and last characters of each input string
Given two strings, s1 and s2, write a program to return a new string made of s1 and s2’s first, middle, and last characters.
Example
Input
s1 = "America" s2 = "Japan"
Output
AJrpan
Hint:
- String index starts with index 0. The first character is present at index 0, and the last character is at the index string’s length -1
- Use built-in function
len(s1)to get the string length. - Next, get the middle index number by dividing string length by 2.
Solution:
- Get the first character from both strings, concatenate them, and store them in variable
x - Get the middle character from both strings, concatenate them, and store them in variable
y - Get the last character from both strings, concatenate them, and store them in variable x
- In the end, join
x,y, andzand save it in the resultvariable - print the
result
def mix_string(s1, s2):
# get first character from both string
first_char = s1[0] + s2[0]
# get middle character from both string
middle_char = s1[int(len(s1) / 2):int(len(s1) / 2) + 1] + s2[int(len(s2) / 2):int(len(s2) / 2) + 1]
# get last character from both string
last_char = s1[len(s1) - 1] + s2[len(s2) - 1]
# add all
res = first_char + middle_char + last_char
print("Mix String is ", res)
s1 = "America"
s2 = "Japan"
mix_string(s1, s2)
Exercise 4: Arrange string characters such that lowercase letters should come first
Given string contains a combination of the lower and upper case letters. Write a program to arrange the characters of a string so that all lowercase letters should come first.
Example
Input
str1 = PyNaTive
Output
yaivePNT
Hint:
- Iterate each character from a string and check if the current character is the lower or upper case using
islower()string function
Solution:
- Create two lists lower and upper
- Iterate a string using a for loop
- In each loop iteration, check if the current character is the lower or upper case using the
islower()string function. - If a character is the lower case, add it to the lower list, else add it to the upper list
- to join the lower and upper list using a
join()function. - convert list to string
- print the final string
str1 = "PYnAtivE"
print('Original String:', str1)
lower = []
upper = []
for char in str1:
if char.islower():
# add lowercase characters to lower list
lower.append(char)
else:
# add uppercase characters to lower list
upper.append(char)
# Join both list
sorted_str = ''.join(lower + upper)
print('Result:', sorted_str)
Exercise 5: Count
Count all letters, digits, and special symbols from a given string
Example
Input
str1 = "P@#yn26at^&i5ve"
Output
Total counts of chars, digits, and symbols Chars = 8 Digits = 3 Symbol = 4
Hint:
- Use the following string functions
isalpha(): To check if a string/character is an alphabetisdigit(): To check if a string/character is a digit.
Solution:
- Iterate each character from a string using a
forloop - In each loop iteration, check if the current character is the alphabet using an
isalpha()function. If yes, increase the character counter. Check if it is a digit using theisdigit()function and increase the digit counter; otherwise, increase the symbol counter. - Print the value of each counter
def find_digits_chars_symbols(sample_str):
char_count = 0
digit_count = 0
symbol_count = 0
for char in sample_str:
if char.isalpha():
char_count += 1
elif char.isdigit():
digit_count += 1
# if it is not letter or digit then it is special symbol
else:
symbol_count += 1
print("Chars =", char_count, "Digits =", digit_count, "Symbol =", symbol_count)
sample_str = "P@yn2at&#i5ve"
print("total counts of chars, Digits, and symbols \n")
find_digits_chars_symbols(sample_str)
Exercise 6: Create a mixed String using the following rules
Given two strings, s1 and s2. Write a program to create a new string s3 made of the first char of s1, then the last char of s2, Next, the second char of s1 and second last char of s2, and so on. Any leftover chars go at the end of the result.
Example
Input
s1 = "Abc" s2 = "Xyz"
Output
AzbycX
Hint:
- Some hint for student
Solution:
s1 = "Abc"
s2 = "Xyz"
# get string length
s1_length = len(s1)
s2_length = len(s2)
# get length of a bigger string
length = s1_length if s1_length > s2_length else s2_length
result = ""
# reverse s2
s2 = s2[::-1]
# iterate string
# s1 ascending and s2 descending
for i in range(length):
if i < s1_length:
result = result + s1[i]
if i < s2_length:
result = result + s2[i]
print(result)
Exercise 7: String characters balance Test
Write a program to check if two strings are balanced. For example, strings s1 and s2 are balanced if all the characters in the s1 are present in s2. The character’s position doesn’t matter.
Example
Input
s1 = "Yn" s2 = "PYnative"
Output
True
Input
s1 = "Ynf" s2 = "PYnative"
Output
False
Hint:
- Iterate each character from a string s1 and check if the current character is present in the string s2.
Solution:
def string_balance_test(s1, s2):
flag = True
for char in s1:
if char in s2:
continue
else:
flag = False
return flag
s1 = "Yn"
s2 = "PYnative"
flag = string_balance_test(s1, s2)
print("s1 and s2 are balanced:", flag)
s1 = "Ynf"
s2 = "PYnative"
flag = string_balance_test(s1, s2)
print("s1 and s2 are balanced:", flag)
Exercise 8: Find all occurrences of a substring in a given string by ignoring the case
Write a program to find all occurrences of “USA” in a given string ignoring the case.
Example
Input
str1 = "Welcome to USA. usa awesome, isn't it?"
Output
The USA count is: 2
Hint:
- Use the string function
count()
Solution:
str1 = "Welcome to USA. usa awesome, isn't it?"
sub_string = "USA"
# convert string to lowercase
temp_str = str1.lower()
# use count function
count = temp_str.count(sub_string.lower())
print("The USA count is:", count)
Exercise 9: Calculate the sum and average of the digits present in a string
Given a string s1, write a program to return the sum and average of the digits that appear in the string, ignoring all other characters.
Example
Input
str1 = "PYnative29@#8496"
Output
Sum is: 38 Average is 6.333333333333333
Hint:
- Iterate each character from a string s1 and check if the current character is digit using the
isdigit()function
Solution:
Solution 1: Use string functions
- Iterate each character from a string
s1using a loop - In the body of a loop, check if the current character is digit using the
isdigit()function - If it is a digit, then add it to the sum variable
- In the end, calculate the average by dividing the total by the count of digits
input_str = "PYnative29@#8496"
total = 0
cnt = 0
for char in input_str:
if char.isdigit():
total += int(char)
cnt += 1
# average = sum / count of digits
avg = total / cnt
print("Sum is:", total, "Average is ", avg)
Solution 2: Use regular expression
import re
input_str = "PYnative29@#8496"
digit_list = [int(num) for num in re.findall(r'\d', input_str)]
print('Digits:', digit_list)
# use the built-in function sum
total = sum(digit_list)
# average = sum / count of digits
avg = total / len(digit_list)
print("Sum is:", total, "Average is ", avg)
Exercise 10: Count occurrences of all characters
Write a program to count occurrences of all characters within a string
Example
Input
str1 = "Apple"
Output
{'A': 1, 'p': 2, 'l': 1, 'e': 1}
Hint:
- Use the string function
count()
Solution:
- Create an empty dictionary to store the result. character is the key, and the count is the value
- Iterate each character from a string
s1using a loop - In the body of a loop, use the
count()function to find how many times a current character appeared in a string - Add key-value pair in a dictionary
str1 = "Apple"
# create a result dictionary
char_dict = dict()
for char in str1:
count = str1.count(char)
# add / update the count of a character
char_dict[char] = count
print('Result:', char_dict)
Exercise 11: Reverse a given string
Full description.
Example
Input
str1 = "PYnative"
Output
evitanYP
Hint:
- Use negative slicing
- Or use the built-in function
reversed().
Solution:
Solution 1: Negative String slicing
str1 = "PYnative"
print("Original String is:", str1)
str1 = str1[::-1]
print("Reversed String is:", str1)
Solution 2: Using the reversed() function
str1 = "PYnative"
print("Original String is:", str1)
str1 = ''.join(reversed(str1))
print("Reversed String is:", str1)
Exercise 12: Find the last position of a given substring
Write a program to find the last position of a substring “Emma” in a given string.
Example
Input
str1 = "Emma is a data scientist who knows Python. Emma works at google."
Output
Last occurrence of Emma starts at index 43
Hint:
- Use the string function
rfind()
Solution:
str1 = "Emma is a data scientist who knows Python. Emma works at google."
print("Original String is:", str1)
index = str1.rfind("Emma")
print("Last occurrence of Emma starts at index:", index)
Run
Exercise 13: Split a string on hyphens
Write a program to split a given string on hyphens and display each substring.
Example
Input
str1 = Emma-is-a-data-scientist
Output
Displaying each substring Emma is a data scientist
Hint:
- Use the string function
split()
Solution:
str1 = "Emma-is-a-data-scientist"
print("Original String is:", str1)
# split string
sub_strings = str1.split("-")
print("Displaying each substring")
for sub in sub_strings:
print(sub)
Exercise 14: Remove empty strings from a list of strings
Full description.
Example
Input
str_list = ["Emma", "Jon", "", "Kelly", None, "Eric", ""]
Output
Original list of sting ['Emma', 'Jon', '', 'Kelly', None, 'Eric', ''] After removing empty strings ['Emma', 'Jon', 'Kelly', 'Eric']
Hint:
- Use the built-in function
filter()to remove empty strings from a list - Or use the for loop and
ifcondition to remove the empty strings from a list
Solution:
Solution 1: Using the loop and if condition
str_list = ["Emma", "Jon", "", "Kelly", None, "Eric", ""]
res_list = []
for s in str_list:
# check for non empty string
if s:
res_list.append(s)
print(res_list)
Solution 2: Using the built-in function filter()
str_list = ["Emma", "Jon", "", "Kelly", None, "Eric", ""]
# use built-in function filter to filter empty value
new_str_list = list(filter(None, str_list))
print("After removing empty strings")
print(new_str_list)
Exercise 15: Remove special symbols / punctuation from a string
Full description.
Example
Input
str1 = "/*Jon is @developer & musician"
Output
"Jon is developer musician"
Hint:
- Use string functions
translate()andmaketrans() - Use the
translate()function to get a new string where specified characters are replaced with the character described in a dictionary or a mapping table. - Use the
maketrans()function to create a mapping table. - Or Use the regex in Python. See Python regex replace.
Solution:
Solution 1: Use string functions translate() and maketrans().
The string.punctuation constant contain all special symbols.
import string
str1 = "/*Jon is @developer & musician"
print("Original string is ", str1)
new_str = str1.translate(str.maketrans('', '', string.punctuation))
print("New string is ", new_str)
Solution 2: Using regex replace pattern in a string
import re
str1 = "/*Jon is @developer & musician"
print("Original string is ", str1)
# replace special symbols with ''
res = re.sub(r'[^\w\s]', '', str1)
print("New string is ", res)
Exercise 16: Removal all characters from a string except integers
Full description.
Example
Input
str1 = 'I am 25 years and 10 months old'
Output
2510
Hint:
- Use the string function
isdigit()
Solution:
str1 = 'I am 25 years and 10 months old'
print("Original string is", str1)
# Retain Numbers in String
# Using list comprehension + join() + isdigit()
res = "".join([item for item in str1 if item.isdigit()])
print(res)
Exercise 17: Find words with both alphabets and numbers
Write a program to find words with both alphabets and numbers from an input string.
Example
Input
str1 = "Emma25 is Data scientist50 and AI Expert"
Output
Emma25 scientist50
Hint:
- Use the built-in function
any()with the combination of string functionsisalpha()andisdigit()
Solution:
str1 = "Emma25 is Data scientist50 and AI Expert"
print("The original string is : " + str1)
res = []
# split string on whitespace
temp = str1.split()
# Words with both alphabets and numbers
# isdigit() for numbers + isalpha() for alphabets
# use any() to check each character
for item in temp:
if any(char.isalpha() for char in item) and any(char.isdigit() for char in item):
res.append(item)
print("Displaying words with alphabets and numbers")
for i in res:
print(i)
Exercise 18: Replace each special symbol with # in the following string
Full description.
Example
Input
str1 = '/*Jon is @developer & musician!!'
Output
##Jon is #developer # musician##
Hint:
- Use string function
replace()
Solution:
- Use the
string.punctuationconstant to get the list of all punctuations - Iterate each symbol from a punctuations
- Use string function
replace()to replace the current special symbol in a string with #
import string
str1 = '/*Jon is @developer & musician!!'
print("The original string is : ", str1)
# Replace punctuations with #
replace_char = '#'
# string.punctuation to get the list of all special symbols
for char in string.punctuation:
str1 = str1.replace(char, replace_char)
print("The strings after replacement : ", str1)
