Python Strings Exercise and Solutions


Question 1 : How can we count all the characters in the string and create a dictionary of the result?

Given String:

str = "I love Logical Python. Python is one of the most used Programming Language."

Expected Output: Count of each character

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

We need to iterate over the string and find the count of each character

str = "I love Logical Python. Python is one of the most used Programming Language."

#unique letters so that we are not looping the long string.
unique_letters = set(str)

letters_count = {}


for letter in unique_letters:
    letters_count[letter] = str.count(letter)
    
print(letters_count)

Output:

{'d': 1, '.': 2, 'I': 1, 'l': 2, 's': 3, 'P': 3, 'v': 1, 'c': 1, 'h': 3, 'f': 1, 'g': 5, 'm': 3, 'y': 2, 'i': 3, 'n': 5, 'a': 4, 't': 4, 'e': 5, 'u': 2, 'L': 2, 'r': 2, ' ': 12, 'o': 8}

[/bg_collapse]


Question 2 : Write a Program to fetch numbers from the string.

Given String:

x = "I love*2 Logical Python * 25"

Expected Output:

225

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

Two ways we can fetch numbers from the string:

1. Using the for loop.

x = "I love*2 Logical Python * 25"

numbers = ""
for i in x:
    if (i.isdigit()):
        numbers = numbers + i

print(numbers)

2. Using the list comprehensions.

x = "I love*2 Logical Python * 25"

list_of_numbers = [i for i in x if i.isdigit()]
numbers = "".join(list_of_numbers)

print(numbers)

Output:

225

[/bg_collapse]


Question 3 : How can we find the first and last occurrence index of the sub string from the string?

Given String:

str = "I love Logical Python. Python is one of the most used Programming Language. That is why i love it."

Expected Output:

2
90

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

For this, we have to use the find and rfind string methods.

str = "I love Logical Python. Python is one of the most used Programming Language. That is why i love it."

print(str.find('love'))
print(str.rfind('love'))

Output:

2
90

[/bg_collapse]


Question 4 : How can we remove the special characters from the string?

Given String:

x = "I love* you #Logical #Python"

Expected Output:

x = I love you Logical Python 

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

Using the string package and for loop.

import string

x = "I love* you #Logical #Python"

print()

for i in string.punctuation:
    if i in x:
        x = x.replace(i,"")
        
print("x =",x)

Output:

x = I love you Logical Python 

[/bg_collapse]


Question 5 : What is the output of the below code? If it is an error, then what changes we can make to fix this error?

Given String:

str = "LogicalPython"
print(str.index("Best"))

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

Answer:

The index() method returns the “substring not found” error if the sub-string is not found in the string. To escape from the error, we can use the find() methods. It will return -1 if the substring is not found.

Using find() method:

str = "LogicalPython"
print(str.find("Best"))

Output:

-1

[/bg_collapse]


Question 6 : Write a program in python to check if the string is a palindrome?

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

Without using slicing:

str = "hannah"
length = len(str)
mid = length//2

for i in range(mid):
    if str[i] != str[-1-i]:
        print("String is not palindrome.")
        break;
else:
    print("String is a palindrome.")

Output:

String is a palindrome.

[/bg_collapse]


Question 7 : Write a program to capitalize every next letter of the string.

Given String:

logical

Output:

LoGiCaL

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

Solution:

str = "logical"

length = len(str)
str2 = ""

for i in range(length):
    if i%2 == 0:
        str2 += str[i].upper()
    else:
        str2 += str[i]

print(str2)

Output:

LoGiCaL

[/bg_collapse]


Question 8 : Write a program to check if the given email belongs to the “@logical.com” domain.

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

Solution:

email = input("Please enter your email id : ")
domain = "@logical.com"
len_email = len(email)
len_domain = len(domain)
if len_email == len_domain:
    print("Please enter complete email address.")
else:
    if email[len_email-len_domain:] == domain:
        print("Your email is valid")
    else:
        print("Your email is invalid")

Output:

Please enter your email id : abc@logical.com
Your email is valid

[/bg_collapse]


Question 9 : Write a program to find all the locations of a character from the string.

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

Solution:

str = input("Please enter the string : ")
character = input("Please enter the character : ")

print("Index Locations:")
for i in range(len(str)):
    if str[i] == character:
        print(i)

Output:

Please enter the string : Logical Python
Please enter the character : o
Index Locations:
1
12

[/bg_collapse]



dinamobet - dinamobet giriş - dinamobet güncel giriş
dinamobet
dinamobet - dinamobet giriş - dinamobet güncel giriş
dinamobet
dinamobet
dinamobet
Dinamobet
betasus
meritking
dinamobet
dinamobet
dinamobet
dinamobet
dinamobet
dinamobet
dinamobet
Dinamobet
Dinamobet
Dinamobet
Dinamobet
Dinamobet
Dinamobet
Kulisbet
Kulisbet
Dinamobet
Dinamobet
Kulisbet
Kulisbet
Kulisbet giriş
Kulisbet güncel giriş