Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
113 views

Python String Exercise With Solutions - String Programs For Practice

python string exercise

Uploaded by

rishankvmadayi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

Python String Exercise With Solutions - String Programs For Practice

python string exercise

Uploaded by

rishankvmadayi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

Search this website Home » Python Exercises » Python String Exercise with
Solutions

Posted In
Python String Exercise
with Solutions
Python Python Basics Python
Exercises

Tweet F share in share P Pin Updated on: October 6, 2021 | + 219 Comments

 Python Exercises As you know, the stings are widely used to hold
textual data. To perform any programming tasks in
 Python Exercises Home
Python, a good understanding of string
 Basic Exercise for Beginners
manipulation is necessary.
 Input and Output Exercise

 Loop Exercise These string exercises will help Python developers

 Functions Exercise to learn and practice string operations,


manipulations, slicing, and string functions.
 String Exercise

 Data Structure Exercise Also Read:


 List Exercise

 Dictionary Exercise
Python String Quiz

 Set Exercise

 Tuple Exercise
This String Exercise includes the following: –
https://pynative.com/python-string-exercise/ 1/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

 Date and Time Exercise

It contains 18 Python string programs,


questions, problems, and challenges to
practice.

The solution is provided for all questions.

All string programs are tested on Python 3

Use Online Code Editor to solve exercise


questions. Let us know if you have any alternative
solutions in the comment section below.

Table of contents

Exercise 1A: Create a string made of the first,


middle and last character

https://pynative.com/python-string-exercise/ 2/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

Exercise 1B: Create a string made of the


middle three characters
Exercise 2: Append new string in the middle of
a given string
Exercise 3: Create a new string made of the
first, middle, and last characters of each input
string
Exercise 4: Arrange string characters such that
lowercase letters should come first
Exercise 5: Count all letters, digits, and special
symbols from a given string
Exercise 6: Create a mixed String using the
following rules
Exercise 7: String characters balance Test
Exercise 8: Find all occurrences of a substring
in a given string by ignoring the case
Exercise 9: Calculate the sum and average of
the digits present in a string
Exercise 10: Write a program to count
occurrences of all characters within a string
Exercise 11: Reverse a given string

World-class AI
https://pynative.com/python-string-exercise/ 3/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

Exercise 12: Find the last position of a given


for writing.
substring
Exercise 13: Split a string on hyphens 30M people use
Grammarly daily to write
Exercise 14: Remove empty strings from a list sharper and work smarter.
of strings Try it for free today.
Exercise 15: Remove special symbols /
Grammarly
punctuation from a string
Exercise 16: Removal all characters from a
string except integers
Learn More
Exercise 17: Find words with both alphabets
and numbers
Exercise 18: Replace each special symbol with
# in the following string

https://pynative.com/python-string-exercise/ 4/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

World-class AI for writing.

Exercise 1A: Create a string made


of the first, middle and last
character

Write a program to create a new string made of an


input string’s first, middle, and last character.

Given:

str1 = "James"

https://pynative.com/python-string-exercise/ 5/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

Expected Output:

Jms

+ Show 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

https://pynative.com/python-string-exercise/ 6/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

World-class AI for writing.

+ Show 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 the result variable

Next, get the middle character’s index by


dividing string length by 2. x = len(str1)
/2 . Use str1[x] to get the middle
character and add it to the result variable

https://pynative.com/python-string-exercise/ 7/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

Use str1[len(str1)-1] to get the last


character of a string and add it to the
result variable

print result variable 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)

  Run

https://pynative.com/python-string-exercise/ 8/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

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.

Given:

Case 1 Case 2

str1 = "JhonDipPeta" str2 = "JaSonAy"

Output Output

Dip Son

https://pynative.com/python-string-exercise/ 9/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

World-class AI for writing.

+ Show Hint

+ Show 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]

https://pynative.com/python-string-exercise/ 10/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

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 char


res = str1[mi - 1:mi + 2]
print("Middle three chars are:", res)

get_middle_three_chars("JhonDipPeta")
get_middle_three_chars("JaSonAy")

  Run

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 .

Given:

https://pynative.com/python-string-exercise/ 11/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

s1 = "Ault"
s2 = "Kelly"

Expected Output:

AuKellylt

+ Show Hint

+ Show Solution

First, get the middle index number of s1 by


dividing s1’s length by 2

Use string slicing to get the character from


s1 starting from 0 to the middle index
number and store it in x

concatenate x and s2 . x = x + s2

concatenate x and remeaning character


from s1

print x

https://pynative.com/python-string-exercise/ 12/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

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 in


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 mi

append_middle("Ault", "Kelly")

  Run

Exercise 3: Create a new string


made of the first, middle, and last
PYnative  Learn Python  Exercises  Quizzes  Code Editor Search this website
Python Programming
characters of each input string

https://pynative.com/python-string-exercise/ 13/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

The latest headlines


from the Financial Times

World-class AI for writing.

Given two strings, s1 and s2 , write a program to


Read article
return a new string made of s1 and s2’s first,
middle, and last characters.

Given:

s1 = "America"
s2 = "Japan"

Expected Output:

AJrpan

https://pynative.com/python-string-exercise/ 14/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

+ Show Hint

+ Show Solution

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.

Given:

str1 = PyNaTive

https://pynative.com/python-string-exercise/ 15/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

World-class AI for writing.

Expected Output:

yaivePNT

+ Show Hint

+ Show Solution

Exercise 5: Count all letters, digits,


and special symbols from a given
https://pynative.com/python-string-exercise/ 16/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

string

Given:

str1 = "P@#yn26at^&i5ve"

Expected Outcome:

Total counts of chars, digits, and symbols

Chars = 8
Digits = 3
Symbol = 4

+ Show hint

https://pynative.com/python-string-exercise/ 17/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

World-class AI for writing.

+ Show Solution

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.

https://pynative.com/python-string-exercise/ 18/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

Given:

s1 = "Abc"
s2 = "Xyz"

Expected Output:

AzbycX

+ Show Solution

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.

Given:

https://pynative.com/python-string-exercise/ 19/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

NEWS

All In 1 Place

Case 1: Case 2:

s1 = "Yn" s1 = "Ynf"
s2 = "PYnative" s2 = "PYnative"

Expected Output: Expected Output:

True False Read article

+ Show Hint

https://pynative.com/python-string-exercise/ 20/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

Iterate each character from a string s1 and check


if the current character is present in the string s2.

+ Show Solution

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.

Given:

str1 = "Welcome to USA. usa awesome, isn't i

Expected Outcome:

https://pynative.com/python-string-exercise/ 21/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

World-class AI for writing.

The USA count is: 2

+ Show Hint

Use the string function count()

+ Show Solution

https://pynative.com/python-string-exercise/ 22/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

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.

Given:

str1 = "PYnative29@#8496"

Expected Outcome:

Sum is: 38 Average is 6.333333333333333

+ Show Hint

Iterate each character from a string s1 and check


if the current character is digit using the
isdigit() function

https://pynative.com/python-string-exercise/ 23/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

+ Show Solution

Grammarly

Exercise 10: Write a program to


count occurrences of all characters
within a string

Given:

str1 = "Apple"

https://pynative.com/python-string-exercise/ 24/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

Expected Outcome:

{'A': 1, 'p': 2, 'l': 1, 'e': 1}

+ Show Hint

Use the string function count()

+ Show Solution

Exercise 11: Reverse a given string

Given:

str1 = "PYnative"

Expected Output:

evitanYP

https://pynative.com/python-string-exercise/ 25/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

World-class AI for writing.

+ Show Hint

Use negative slicing

Or use the built-in function reversed() .

+ Show Solution

Exercise 12: Find the last position


of a given substring

https://pynative.com/python-string-exercise/ 26/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

Write a program to find the last position of a


substring “Emma” in a given string.

Given:

str1 = "Emma is a data scientist who knows P

Expected Output:

Last occurrence of Emma starts at index 43

+ Show Hint

Use the string function rfind()

+ Show Solution

Exercise 13: Split a string on


hyphens

https://pynative.com/python-string-exercise/ 27/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

Write a program to split a given string on hyphens


and display each substring.

World-class AI for writing.

Given:

str1 = Emma-is-a-data-scientist

Expected Output:

Displaying each substring

Emma
is

https://pynative.com/python-string-exercise/ 28/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

a
data
scientist

+ Show Hint

Use the string function split()

+ Show Solution

Exercise 14: Remove empty strings


from a list of strings

Given:

str_list = ["Emma", "Jon", "", "Kelly", None

Expected Output:

https://pynative.com/python-string-exercise/ 29/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

Grammarly

Original list of sting


['Emma', 'Jon', '', 'Kelly', None, 'Eric',

After removing empty strings


['Emma', 'Jon', 'Kelly', 'Eric']

+ Show Hint

Use the built-in function filter() to


remove empty strings from a list

https://pynative.com/python-string-exercise/ 30/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

Or use the for loop and if condition to


remove the empty strings from a list

+ Show Solution

Exercise 15: Remove special


symbols / punctuation from a
string

Given:

str1 = "/*Jon is @developer & musician"

Expected Output:

"Jon is developer musician"

+ Show Hint

Use string functions translate() and


maketrans()

https://pynative.com/python-string-exercise/ 31/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

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.

+ Show Solution

World-class AI for writing.

https://pynative.com/python-string-exercise/ 32/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

Exercise 16: Removal all characters


from a string except integers

Given:

str1 = 'I am 25 years and 10 months old'

Expected Output:

2510

+ Show Hint

Use the string function isdigit()

+ Show Solution

Exercise 17: Find words with both


alphabets and numbers

https://pynative.com/python-string-exercise/ 33/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

Write a program to find words with both alphabets


and numbers from an input string.

Given:

str1 = "Emma25 is Data scientist50 and AI Ex

Expected Output:

Emma25
scientist50

World-class AI for writing.

https://pynative.com/python-string-exercise/ 34/182
4/4/24, 7:23 PM Python String Exercise with Solutions – String Programs for Practice

+ Show Hint

Use the built-in function any() with the


combination of string functions isalpha() and
isdigit()

+ Show Solution

Exercise 18: Replace each special


symbol with # in the following
string

Given:

str1 = '/*Jon is @developer & musician!!'

Expected Output:

##Jon is #developer # musician##

https://pynative.com/python-string-exercise/ 35/182

You might also like