## Python String Exercises with Solution

Python String Exercises: String is a data type that is widely used in every programming language for performing the operation on textual data.

These programming exercises have been structured and designed in a beginner-friendly way, focusing on the core concepts of string data type operations.

The main idea behind solving these questions is to make your concept more clear and to improve logical thinking of how to approach a problem. We are going to cover string operations using some mix of conditional statements i.e if, if-else, for loop(), range(), while loop() etc.

For reference ### Python String Exercises for beginners with Solution:

We will solve 10 python string exercises for beginners in python with a solution & detailed code explanation.

### Exercise 1: Write a program to reverse a string in python.

Hint
Input: python

Expected output
Result is: nohtyp

```# WAP to reverse a string
str = input("Input a word to reverse: ")

for i in range(len(str) - 1, -1, -1):
print(str[i], end="")
print("\n")

# output
# nohtyp```

### Exercise 2: Write a program to count vowels and consonants in a string.

Hint
Input: python

Expected output
Vowels count is: 1
Consonant count is: 5

```# WAP to count vowels and consonant
n = input("Enter a Word ")
vow = 0
con = 0
for i in range(len(n)):
if n[i] in ['a','e','i','o','u']:
vow = vow + 1
else:
con = con + 1
print("Total vowels are:", vow)
print("Total conconants are:", con)```

### Exercise 3: Write a program to remove duplicates in a string.

Hint
Input: pythonlobby

Expected output
Result is: p y t h o n l b

```# WAP to remove duplicates in a string
n = input("Enter a Word ")
x = []
for i in range(len(n)):
if n[i] not in x:
x.append(n[i])
else:
pass
for i in range(len(x)):
print(x[i], end=" ")```

### Exercise 4: Write a program to count the number of letters in a word.

Hint
Input: pythonlobby

Expected output
Result is: 11

```# WAP to remove duplicates in a string
n = input("Enter a string ")
count = 0
for i in range(len(n)):
count = count + 1
print(count)```

### Exercise 5: Python program to count the occurrence of each character in a word.

Hint
Given x = programm

Expected output
Occurrence of each characters is :
{‘P’: 1, ‘r’: 2, ‘o’: 1, ‘g’: 1, ‘a’: 1, ‘m’: 2}

```strr = "Programm"

# empty dictionary
dic = {}

for ele in strr:
if ele in dic:
dic[ele] += 1
else:
dic[ele] = 1

# result
print("Occurrence of each characters is :\n " + str(dic))```

### Exercise 6: Python program to convert lower letter to upper and upper letter to lower in a string.

Hint
Inut: PrOgRaMM

Expected output
Result is: pRoGrAmm

```num  = input("Enter a String ")
x = []
for i in range(len(num)):
if num[i].isupper():
x.append(num[i].lower())
elif num[i].islower():
x.append(num[i].upper())

# printing result
for i in range(len(x)):
print(x[i], end=' ')```

### Exercise 7: Python program to search a specific word in a string.

Hint
Input:
Enter a String:  I am a boy
Enter a word to search: boy

Expected output
boy exists in string

Hint
Input:
Enter a String:  I am a boy
Enter a word to search: girl

Expected output
girl not exists in string

```num = input("Enter a string ").split()
search = input("Enter a word to search ")
if search in num:
print(f"{search} exist in String")
else:
print(f"{search} not exist in String")```

### Exercise 8: Write a python program to sort letters of word by lower to upper case format.

Hint
Input:
Enter a String:  pytHOnloBBy

Expected output
Result: p y t n l o y H O B B

```# string initialization
strn = 'pytHOnloBBy'
lower = []
upper = []
for i in range(len(strn)):
if strn[i].islower():
lower.append(strn[i])
else:
upper.append(strn[i])

# printing result
result = ' '.join(lower+upper)
print(result)```

### Exercise 8: Write a program in Python to count lower, upper, numeric and special characters in a string.

Hint
Given x = @pyThOnlobb!Y34

Expected output
Numeric counts 2
Lower counts 8
Upper counts 3
Special counts 2

```# String initialization
n = '@pyThOnlobb!Y34'
numeric = 0
lower = 0
upper = 0
special = 0

for i in range(len(n)):
if n[i].isnumeric():
numeric = numeric + 1
elif n[i].islower():
lower = lower+1
elif n[i].isupper():
upper = upper+1
else:
special = special + 1

# printing result
print("Numeric counts",numeric)
print("Lower counts",lower)
print("Upper counts",upper)
print("Special counts",special)```

### Exercise 9: Write a program in Python to remove an empty character from a list sequence.

Hint
Given n = [“name”,”age”,””,”hello”]

Expected output
Result:  [‘name’, ‘age’, ‘hello’]

```# String initialization
n = ["name","age","","hello"]

# There is a inbuilt list function filter
# We filtered out the None i.e. empty val from sequence of list
new_str = list(filter(None, n))
print(new_str)

# Output
# ['name', 'age', 'hello']```

### Exercise 10: Python program to convert all the starting letter of a word in upper case format or in the title format.

Hint
Given n = “hello this is pythonlobby”

Expected output
Result: Hello This Is Pythonlobby

```# WAP to convert all the starting letter of words in upper case
n = "hello this is pythonlobby"
new_str = n.title()

# getting result
print(new_str)```