Programmer Coding

Python – String Exercises

Python – String Exercises

This page is your ticket to becoming an expert in working with languages ​​and scripts in Python. Whether you’re just starting out or want to advance, we’re here for you.

Strings are like mathematical expressions that Python understands. They are very important! We’ll start with the easy stuff and work our way up to the cooler, more advanced stuff.

1. Write a program that takes two numbers as input and prints their sum.

num1 = int(input(“Enter the first number: “))
num2 = int(input(“Enter the second number: “))
print(“The sum is : “, num1+num2)

Output

Enter the first number: 58
Enter the second number: 14
The sum is : 72

2. Write a program that takes a string as input and prints its length.

text = input(“Enter a string: “)
print(‘The length of the entered string is : ‘, len(text))

3. Write a program that takes a sentence and then prints the number of words in that sentence.

sentence = input(“Enter a sentence: “)py
print(“Number of words in the sentence: “,sentence.count(‘ ‘)+1)

Output

Enter a string: Ramkrishna kumar
The length of the entered string is : 16

4. Write a program that takes a string as input and counts the number of uppercase letters in the string.

x = input(“Enter a string: “)
upper = 0
lower = 0

for i in x:
if i.isupper():
upper += 1
if i.islower():
lower += 1

print(“Upper:”, upper)
print(“Lower:”, lower)

Output

Enter a string: Rohan kumar
Upper: 1
Lower: 9

5. Write a program that takes a sentence and then converts all the characters to uppercase.

sentence = input(“Enter a sentence: “)
print(sentence.upper())

Output

Enter a sentence: Mausam Mishra
MAUSAM MISHRA

6. Write a program that takes a string as input and replaces all occurrences of the letter ‘a’ with the letter ‘e’.

word = input(“Enter a string: “)
rep = word.replace(‘a’,’e’)
print(rep)

Output

Enter a string: Satyam Keshri
Setyem Keshri

7. Create a program that checks if two given strings are anagrams of each other.

word1 = input(“Enter the first word: “)
word2 = input(“Enter the second word: “)

if sorted(word1.replace(” “,””).lower()) == sorted(word2.replace(” “,””).lower()):
print(“The given words are anagrams”)
else:
print(“The given words are not anagrams”)

Output

Enter the first word: Akash
Enter the second word: Raz
The given words are not anagrams

8. Create a program that checks if a given string is a palindrome.

word = input(“Enter the string: “)
if word.replace(” “,””).lower() == word[::-1].replace(” “,””).lower():
print(“The given string is palindrome”)
else:
print(“The given string is not palindrome”)

Output

Enter the string: Nayan
The given string is palindrome

9. Create a program that checks if a substring is present in a given string.

word = input(“Enter a string: “)
sub = input(“Enter the substring to check: “)

if sub in word:
print(“The substring is present in the string”)
else:
print(“The substring is not present in the string”)

Output

Enter a string: Ramkrishna
Enter the substring to check: Ramkrishna
The substring is present in the string

10. Write a program that reverses the order of words in a given sentence.

sentence = input(“Enter a sentence: “)
words = sentence.split(” “)
words.reverse()
newsent = “”
for i in words:
newsent += i + ” ”
print(newsent)

Output

Enter a sentence: Sonu kumar Gupta
Gupta kumar Sonu

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top