Basic Programs of String Datatype in Python (solved) -part 1

Share with others

Q1. Write a program to accept a string and display its length.

```Show AnswerAns.

str = input("Enter any String")
print(len(str))```

Q2. Write a program to accept a string and count number of vowels.

```Show AnswerAns.

str = input("Enter any String")
s1="aeiouAEIOU"
v=0
for i in str:
if i in s1:
v=v+1
print(v)```

Q3. Write a program to accept a string and display it in reverse.

```Show AnswerAns

str = input("Enter any String")
print(str[: :-1])```

Q4. Write a program to check whether an input string is palindrome or not.

```Show AnswerAns.

str = input("Enter any String")
strrev =""
for i in range(len(str),0,-1):
strrev=strrev + str[i-1]
if str.lower() == strrev.lower():
print("Palindrome")
else:
print("Not a palindrome")```

Q5. Write a program to count number of small case alphabets in String.

```Show AnswerAns.

str = input("Enter any String")
L=0
for i in str:
if i.islower( ):
L = L+1
print("Total small case alphabets are : ",l)```

Q6. Write a program to count the number of words in the input string.

```Show AnswerAns.

str = input("Enter any String")
L=str.split()
print("Total number of words are ", len(L))```

Q7. Write a program to accept a string from the user and count the frequency of alphabet ‘a’ and ‘c’.

```Show AnswerAns.

str = input("Enter any String")
a=0
c=0
for i in str:
if i=='a':
a+=1
if i=='c':
c+=1
print("Total number of alphabet 'a' is ", a)
print("Total number of alphabet 'c' is ", c)```

Q8. Write a program to display the last word of the string accepted from user.

```Show AnswerAns.

str = input("Enter any String")
w=str.split()
print(w[-1])```

Q9. Write a program to display those words from the string in python which are starting from alphabet ‘a’ or ‘A”.

```Show AnswerAns.

str = input("Enter any String")
w=str.split()
c=0
for i in w:
if i[0]=='a' or i[0]=='A':
c=c+1
print("Total words starting from 'a' or 'A' : ",c)```

Q10. Write a program to count number of digits in a string accepted from user.

```Show AnswerAns.

str = input("Enter any String")
d=0
for i in str:
if i.isdigit():
d=d+1
print("Total digits are : ",d)```

Disclaimer : I tried to give you the correct code of all the above “String Datatype Python Practice ” programs, but if you feel that there is/are mistakes in the code given above, you can directly contact me at csiplearninghub@gmail.com. Also Share your feedback so that I can give better content to you.

Class 12 Computer Science Sample Paper 2020-2021.

Class 12 Computer Science Sample Paper Marking Scheme

Class 12 Computer Science Test Series

Share with others
error: Content is protected !!