PYTHON STRINGS
Q1. Write Python script to input a string and a character and count the
number of occurrences of the character in the string.
Show/Hide Solution
str=input("Enter a string")
chr=input("Enter a character")
c=[Link](chr)
print("Total number of occurrences are:", c)
Q2. Write Python script to input a string and display it in reverse order.
Show/Hide Solution
str=input("Enter a string")
rev=str[::-1]
print("Reversed string is:", rev)
Q3. Write Python script to input a string and check whether it is a palindrome
or not.
Show/Hide Solution
str=input("Enter a string")
rev=str[::-1]
if str==rev:
print("Palindrome")
else:
print("Not a palindrome")
Q4. Write Python script to input a string and count the number of words in it.
Show/Hide Solution
str=input("Enter a string")
c=[Link](" ")
print("No. of words are:",c+1)
Q5. Write Python script to input a string and count the number of words
beginning with 'A' or 'a'.
Show/Hide Solution
str=input("Enter a string")
x=[Link](" A")
y=[Link](" a")
if str[0]=="a":
y=y+1
if str[0]=="A":
x=x+1
print("No. of words starting with A are:",x)
print("No. of words starting with a are:",y)
Q6. Write Python script to input a string and replace the first letter of every
word to uppercase and then display it.
Show/Hide Solution
str=input("Enter a string")
x=[Link]()
print(x)
Q7. Write Python script to input a string and replace all occurrences of the
word ‘the’ with ‘that’.
Show/Hide Solution
str=input("Enter a string")
x=[Link]("the","that")
print(x)
Q8. Write Python script to input a string and count and display the number of
capital alphabets, small alphabets and numbers.
Show/Hide Solution
str=input("Enter a string")
x,y,z=0,0,0
for i in str:
if [Link]():
x=x+1
elif [Link]():
y=y+1
elif [Link]():
z=z+1
print("Upper case: ", x)
print("Lower case: ", y)
print("Numbers: ", z)