Python : String - Exercises and Solution


1. Write a Python program to calculate the length of a string.

Sample Output

Tutor Joes

Length of String = 10

View Solution

2. Write a Python program to count the number of characters (character frequency) in a string.

Sample Output

tutorjoes

{'t' : 2, 'u' : 1, 'o' : 2, 'r' : 1, 'j' : 1, 'e' : 1, 's' : 1}

View Solution

3. Write a Python program to get a string from a given string where all occurrences of its first char have been changed to '@', except the first char itself.

Sample Output

Given String = tutor joes

After String = tu@or joes

View Solution

4. Write a Python program to get a single string from two given strings, separated by a space and swap the first two characters of each string.

Sample Output

Before Swap = abc xyz

After Swap = xyc abz

View Solution

5. Write a Python program to remove the nth index character from a nonempty string.

Sample Output

Before Remove = Tutor Joes

Remove character = t

After Remove = Tuor Joes

View Solution

6. Write a Python program to change a given string to a new string where the first and last chars have been exchanged

Sample Output

Before Exchange = Python

After Exchange = nythoP

View Solution

7. Write a Python program to remove the characters which have odd index values of a given string

Sample Output

String = Computer Education

Remove Odd Index Char = Cmue dcto

View Solution

8. Write a Python program to count the occurrences of each word in a given sentence

Sample Output

string = To change the overall look your document. To change the look available in the gallery

{ 'To': 2, 'change': 2, 'the': 3, 'overall': 1, 'look': 2, 'your': 1, 'document.': 1, 'available': 1, 'in': 1, 'gallery': 1 }

View Solution

9. Write a Python script that takes input from the user and displays that input back in upper and lower cases.

Sample Output

Enter the String = Tutor Joes

Uppercase = TUTOR JOES

Lowercase = tutor joes

View Solution

10. Write a Python function to reverses a string if it's length is a multiple of 4.

Sample Output

Enter the String = Computer

If the length is a multiple of 4 = retupmoC

Enter the String = Science

If the length is not a multiple of 4 = Science

View Solution

11. Write a Python function to convert a given string to all uppercase if it contains at least 2 uppercase characters in the first 4 characters.

Sample Output

Enter the String = ComPuTeR

Check if there are at least 2 uppercase letters in the first 4 characters = COMPUTER

Enter the String = Computer

Check if does not contain at least 2 uppercase letters in the first 4 characters = Computer

View Solution

12. Write a Python program to sort a string lexicographically.

Sample Output

Enter the String = Tutor

['o', 'r', 'T', 't', 'u']

View Solution

13. Write a Python program to remove a newline in Python.

Sample Output

String = "\nTutor \nJoes \nComputer \nEducation\n"

Before Remove NewLine(\n)
Tutor
Joes
Computer
Education

After Remove NewLine(\n) = Tutor Joes Computer Education

View Solution

14. Write a Python program to check whether a string starts with specified characters.

Sample Output

Tutor Joes Computer Education

Prefix to check = Tu

Check if the input string starts with the prefix = True

View Solution

15. Write a Python program to display formatted text (width=35,70) as output.

Sample Output

Python is an interpreted, object-oriented, high-level programming language
that can be used for a wide variety of applications. Python is a powerful
general-purpose programming language.

Formatted Text (Width 35) :
Python is an interpreted, object-
oriented, high-level programming
language that can be used for a
wide variety of applications.
Python is a powerful general-purpose
programming language.

Formatted Text (Width 70) :
Python is an interpreted, object-oriented, high-level programming language
that can be used for a wide variety of applications. Python is a powerful
general-purpose programming language.

View Solution

16. Write a Python program to remove existing indentation from all of the lines in a given text.

Sample Output

Original text :
              Python is an interpreted, object-oriented, high-level programming language
              that can be used for a wide variety of applications. Python is a powerful
              general-purpose programming language.

Dedented text :
Python is an interpreted, object-oriented, high-level programming language
that can be used for a wide variety of applications. Python is a powerful
general-purpose programming language.

View Solution

17. Write a Python program to add a prefix text to all of the lines in a string.

Sample Output

Text :
              Python is an interpreted, object-oriented, high-level programming language
              that can be used for a wide variety of applications. Python is a powerful
              general-purpose programming language.

Prefix to add to each line = *

Display the Result :

      *  Python is an interpreted, object-oriented, high-level programming language
      *  that can be used for a wide variety of applications. Python is a powerful
      *  general-purpose programming language.

View Solution

18. Write a Python program to print the following floating numbers upto 2 decimal places.

Sample Output

Floating Point Number = 23.36778

Print Number with two Decimal Places = 23.36

View Solution

19. Write a Python program to print the following floating numbers upto 2 decimal places with a sign

Sample Output

Floating Point Number1 = -84.99

Print Number with a sign and two decimal places = -84.99

Floating Point Number2 = 23.36778

Print Number with a sign and two decimal places = +23.37

View Solution

20. Write a Python program to print the following floating numbers with no decimal places

Sample Output

Floating Point Number = 23.36778

No Decimal Places = 23

View Solution

21. Write a Python program to print the following integers with zeros on the left of specified width

Sample Output

Number1 = 23

Formatted Number1(left padding, width 2) = 23

Number2 = 833

Formatted Number2(left padding, width 6) = 000833

View Solution

22. Write a Python program to reverse a string.

Sample Output

String = Python Exercises

Reverse String = sesicrexE nohtyP

View Solution

23. Write a Python program to reverse words in a string.

Sample Output

String = Tutor Joes Computer Educations

Reverse Words = Educations Computer Joes Tutor

View Solution

24. Write a Python program to strip a set of characters from a string.

Sample Output

String = Tutor Joes Computer Education

After stripping aeiou AEIOU = T t r    J s    C m p t r    d c t n

View Solution

25. Write a Python program to convert a given string into a list of words.

Sample Output

Tutor Joes Computer Education

[ 'Tutor', 'Joes', 'Computer', 'Education' ]

View Solution

26. Write a Python program to swap comma and dot in a string.

Sample Output

Before Swap Comma and Dot = 23,2000.00

After Swap Comma and Dot = 23.2000,00

View Solution

27. Write a Python program to count and display the vowels of a given text.

Sample Output

Text = Tutor Joes

Vowels Character = [ 'u' , 'o' , 'o' , 'e' ]

Vowels Count = 4

View Solution

28. Write a Python program to split a string on the last occurrence of the delimiter.

Sample Output

String = "T,u,t,o,r,J,o,e,s"

Delimiter = ","

['T,u,t,o,r', 'J', 'o', 'e', 's']

['T,u,t,o,r,J,o', 'e', 's']

View Solution

29. Write a Python program to find the first non-repeating character in given string.

Sample Output

String = Tutor Joes

T
u
t
r
J
e
s

View Solution

30. Write a Python program to find the first repeated word in a given string.

Sample Output

String = Hello world ! Hello Tutor Joes

First Repeated Word = Hello

View Solution

31. Write a Python program to remove spaces from a given string.

Sample Output

String = Tutor Joes Computer Education

Remove Spaces = TutorJoesComputerEducation

View Solution

32. Write a Python program to remove duplicate characters of a given string.

Sample Output

String = String and String Function

Remove Duplicate Characters in String = String adFuco

View Solution

33. Write a Python program to compute sum of digits of a given string.

Sample Output

String = Hello World 2345

Sum of Digits of a Given String = 14

View Solution

34. Write a Python program to remove all consecutive duplicates of a given string

Sample Output

string = AAAABBBBCCCC

Removing Consecutive Duplicates = ABC

View Solution

35. Write a Python program to create a string from two given strings concatenating uncommon characters of the said strings

Sample Output

String1 = ABCPQXYZ

String2 = XYNZABMC

Concatenating Uncommon Characters = PQNM

View Solution

36. Write a Python program to move all spaces to the front of a given string in single traversal.

Sample Output

String = Tutor Joes Computer Education

After Moving all Spaces to the Front =        TutorJoesComputerEducation

View Solution

37. Write a Python program to count Uppercase, Lowercase, special character and numeric values in a given string

Sample Output

Strings = Hell0 W0rld ! 123 * #

UpperCase = 2

LowerCase = 6

NumberCase = 5

SpecialCase = 8

View Solution

38. Write a Python program to count number of non-empty substrings of a given string.

Sample Output

formula = ( ( length of the string ) * ( length of the string + 1 ) / 2 )

Input a string = Joes

Number of substrings = 10

View Solution

39. Write a Python program to find smallest and largest word in a given string.

Sample Output

Strings = Tutor Joes Computer Education

Smallest word = Joes

Largest word = Education

View Solution

40. Write a Python program to count number of substrings with same first and last characters of a given string.

Sample Output

Enter The String = Python

Number of substrings with the same first and last characters = 6

View Solution

41. Write a Python program to find the index of a given string at which a given substring starts. If the substring is not found in the given string Not found.

Sample Output

String = Python Exercises

Substring to Find = EX

Index of the Substring = 7

View Solution

42. Write a Python program to wrap a given string into a paragraph of given width.

Sample Output

Enter a String = Tutor Joes Computer Education

Enter the Width of the Paragraph = 5

Tutor
Joes
Compu
ter E
ducat
ion

View Solution

43. Write a Python program to print four values decimal, octal, hexadecimal (capitalized), binary in a single line of a given integer.

Sample Output

Input an integer = 10

Decimal = 10

Octal = 12

Hexadecimal (capitalized) = A

Binary = 1010

View Solution

44. Write a Python program to swap cases of a given string.

Sample Output

String = Tutor Joes Computer Education

Swap Cases = tUTOR jOES cOMPUTER eDUCATION

View Solution

45. Write a Python program to delete all occurrences of a specified character in a given string.

Sample Output

String = Tutor Joes Computer Education

Character to Delete = 't'

Remove All Occurrences = Tuor Joes Compuer Educaion

View Solution

46. Write a Python program find the common values that appear in two given strings.

Sample Output

String1 = UpperCase

String2 = LowerCase

Common Values = erCase

View Solution

47. Write a Python program to check whether a given string contains a capital letter, a lower case letter, a number and a minimum length.

Sample Output

Enter the string = Hello123
Valid string

Enter the string = GoodEvening
String must have 1 Number.
String length should be at least 8.

Enter the string = 12345
String must have 1 Uppercase Character.
String must have 1 Lowercase Character.
String length should be at least 8.

Enter the string = Python
String must have 1 Number.
String length should be atleast 8.

View Solution

48. Write a Python program to remove duplicate words from a given string.

Sample Output

String = Python Exercises Practice Solution Exercises

After Removing Duplicate = Python Exercises Practice Solution

View Solution

49. Write a Python program to split a given multiline string into a list of lines.

Sample Output

String = "Tutor\nJoes\nComputer\nEducation"

['Tutor', 'Joes', 'Computer', 'Education']

multiline_string = """ Tutor
                                          Joes
                                          Computer
                                          Education """

['Tutor', 'Joes', 'Computer', 'Education']

View Solution

50. Write a Python program to remove punctuations from a given string.

Sample Output

String = Remove. punctuations @from a% given string?

After removing Punctuations = Remove punctuations from a given string

View Solution

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial