STRINGS IN PYTHON
[Link] of strings.
[Link] indexing.
[Link] indexing.
[Link] comparison in python.
[Link].
BOOT CAMP ON STRINGS
1. Write a program to display the largest word from the string.
Ans.
str=input("Enter any String :")
L = [Link]()
max=0
b=""
for i in L:
if len(i) > max:
b=i
max=len(i)
print("Longest word is ",b)
2. Write a program to accept a string and display it in upper case.
Ans.
str=input("Enter any String :")
print([Link]( ))
3. Write a program to display the unique words from the string.
Ans.
str=input("Enter any String :")
L=[Link]( )
L1=[ ]
for i in L:
if i not in L1:
[Link](i)
str=""
for i in L1:
str=str+" "+i
print("String with unique words are :",str)
4. Write a program to accept a string and display the ascii value of
each character.
Ans.
str=input("Enter any String :")
for i in str:
print("ASCII value of ",i,"is",ord(i))
5. Write a program to accept a string and replace all spaces by ‘#’
symbol.
Ans.
str=input("Enter any String :")
str1=""
for i in str:
if [Link]():
str1=str1+'#'
else:
str1=str1+i
print("Output String is :",str1)
#Execution of program
Enter any String :practice string programs
Output String is : practice#string#programs
6. Write a program to accept two strings from the user and display
the common words.(Ignore case)
Ans.
str1=input("Enter first String :")
str2=input("Enter second String :")
L1=[Link]()
L2=[Link]()
for i in L1:
if i in L2:
print(i)
#Execution of program is :
Enter first String : program string programs
Enter second String : program
Program
7. Write a program to accept a string and count the frequency of
each vowel.
Ans
str1=input("Enter String :")
print("frequency of vowel 'a' is :",[Link]('a'))
print("frequency of vowel 'e' is :",[Link]('e'))
print("frequency of vowel 'i' is :",[Link]('i'))
print("frequency of vowel 'o' is :",[Link]('o'))
print("frequency of vowel 'u' is :",[Link]('u'))
#Execution of program
Enter String :Important String Programs
frequency of vowel 'a' is : 2
frequency of vowel 'e' is : 0
frequency of vowel 'i' is : 1
frequency of vowel 'o' is : 2
frequency of vowel 'u' is : 0
8. Write a program to display the smallest word from the string.
Ans.
str=input("Enter any String :")
L = [Link]()
min=50 #we think that length of any word in the string is not larger
than 50 (can give any other number)
b=""
for i in L:
if len(i) < min:
b=i
min=len(i)
print("Smallest word is : ",b)
#Execution of program:
Enter any String :important string programs
Smallest word is : string
9. Write a program to accept a string and display the string in Title
case. (first alphabet of each word in upper case)
Ans.
str=input("Enter any String :")
print([Link]())
#Execution of program is :
Enter any String :my name is amit
My Name Is Amit
10. Write a program to accept a string and display the string with
changed case.(Change upper case alphabet to lower case and
vice versa)
Ans.
str=input("Enter any String :")
print([Link]())
#Execution of program is :
Enter any String :My name is Amit
mY NAME IS aMIT
11. 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"
Expected Output:
Jms
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)
12. Split a string on hyphens
Write a program to split a given string on hyphens and display
each substring.
Given:
str1 = Emma-is-a-data-scientist
Expected Output:
Displaying each substring
Emma
is
a
data
scientist
Show Hint
Show Solution
str1 = "Emma-is-a-data-scientist"
print("Original String is:", str1)
# split string
sub_strings = [Link]("-")
print("Displaying each substring")
for sub in sub_string print(sub)
13. 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
Show Solution
Solution 1: Use string functions
input_str = "PYnative29@#8496"
total = 0
cnt = 0
for char in input_str:
if [Link]():
total += int(char)
cnt += 1
# average = sum / count of digits
avg = total / cnt
print("Sum is:", total, "Average is ", avg)
14. 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.
str1 = "Welcome to USA. usa awesome, isn't it?"
Expected Outcome:
The USA count is: 2
str1 = "Welcome to USA. usa awesome, isn't it?"
sub_string = "USA"
# convert string to lowercase
temp_str = [Link]()
# use count function
count = temp_str.count(sub_string.lower())
print("The USA count is:", count)
15. 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.
Given:
s1 = "Abc"
s2 = "Xyz"
Expected Output:
AzbycX
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)
16. Count all letters, digits, and special symbols from a given string
Given:
str1 = "P@#yn26at^&i5ve"
Expected Outcome:
Total counts of chars, digits, and symbols
Chars = 8
Digits = 3
Symbol = 4
Ans:
def find_digits_chars_symbols(sample_str):
char_count = 0
digit_count = 0
symbol_count = 0
for char in sample_str:
if [Link]():
char_count += 1
elif [Link]():
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)