Stringpractice
Stringpractice
3. Write a Python program to get a string made of the first 2 and last 2 characters of a given string. If the
string length is less than 2, return the empty string instead.
4. 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.
Test cases:
a. both empty string input b. 'a' , 'abc' c. '' 'abc' d. '123' 'wow'
6. Write a Python program to add 'ing' at the end of a given string (length should be at least 3). If the
given string already ends with 'ing', add 'ly' instead. If the string length of the given string is less than 3,
leave it unchanged.
7. Write a Python program to find the first appearance of the substrings 'not' and 'poor' in a given string.
If 'poor' follows 'not', replace the whole 'not'...'poor' substring with 'good'. Return the resulting string.
Test cases:
a. i/p: "not so poor lyrics, try next time"
8. Write a Python program that takes a list of words and return the longest word and the length of the
longest one.
Sample Output:
9. Write a Python
program to remove the nth index character from a nonempty string.
10. Write a Python program to change a given string to a newly string where the first and last chars have
been exchanged.
11. Write a Python program to remove characters that have odd index values in a given string.
13. Write a Python script that takes input from the user and displays that input back in upper and lower
cases.
14. Write a Python program that accepts a comma-separated sequence of words as input and prints the
distinct words in sorted form (alphanumerically).
17. Write a Python function to get a string made of 4 copies of the last two characters of a specified string
(length must be at least 2).
19. Write a Python program to get the last part of a string before a specified character.
22.Write a Python program to sort a string lexicographically.(do both using library and without library)
23. Write a Python program to remove a newline in Python.(do both using library and without library)
24. Write a Python program to check whether a string starts with specified characters.(do both using
library and without library)
Note : In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or
Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of
substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of
positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would
become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.
Nature's gifts are for everyone and many generations. Every generation,
width=50
output:(-> and <- arrow are shown to highlight start and end of line)
27. Write a Python program to remove existing indentation from all of the lines in a given text.
28. Write a Python program to add prefix text to all of the lines in a string.(do both using library and
without library)
Example:
29. Write a Python program to set the indentation (4 spaces) of the first line, from input string containing
a paragraph of text.
Sample input
Nature's gifts are for everyone and many generations. Every generation,
Nature's gifts are for everyone and many generations. Every generation,
30. Write a Python program to print the following numbers up to 2 decimal places.
a. 123.45678
b. 0.0000004
c. 123
d. 123456789.9
31. Write a Python program to print the following numbers up to 2 decimal places with a sign.
a. 123.45678
b. -0.0000004
c. 123
d. -123456789.9
32. Write a Python program to print the following positive and negative numbers with no decimal places.
a. 123.45678
b. -0.0000004
c. 123
d. -123456789.9
33. Write a Python program to print the following integers with zeros to the left of the specified width. If
integer with greater width, then display error message.
a. Use format
Example : width =5
input output
345 00345
56789 56789
0 00000
123456 Error
34. Write a Python program to print the following integers with '*' to the right of the specified width. If
integer with greater width, then display error message.
a. Use format
Example : width =5
input output
345 345**
56789 56789
0 0****
123456 Error
35. Write a Python program to display a number with a comma separator. After every thousand there
should be comma added. (do both using library and without library)
Ex.
input output
0 0
-123456 -123,456
123456789 123,456,789
9876 9,876
78 78
36. Write a Python program to format a number with a percentage.(do both using library and without
library)
37. Write a Python program to display a number in left, right, and center aligned with a width of 10.
38. Write a Python program to count occurrences of a substring in a string.(do both using library and
without library)
39. Write a Python program to reverse a string.(do both using library,using slice operator and without
library)
40. Write a Python program to reverse all words in a string.(do both using slice and without library)
Ex
4. 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.
5. 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.
Test cases:
a. both empty string input b. 'a' , 'abc' c. '' 'abc' d. '123' 'wow'
6. Write a Python program to add 'ing' at the end of a given string (length should be at least 3). If the
given string already ends with 'ing', add 'ly' instead. If the string length of the given string is less than 3,
leave it unchanged.
7. Write a Python program to find the first appearance of the substrings 'not' and 'poor' in a given string.
If 'poor' follows 'not', replace the whole 'not'...'poor' substring with 'good'. Return the resulting string.
Test cases:
8. Write a Python program that takes a list of words and return the longest word and the length of the
longest one.
Sample Output:
9. Write a Python program to remove the nth index character from a nonempty string.
10. Write a Python program to change a given string to a newly string where the first and last chars have
been exchanged.
11. Write a Python program to remove characters that have odd index values in a given string.
12. Write a Python program to count the occurrences of each word in a given sentence.
13. Write a Python script that takes input from the user and displays that input back in upper and lower
cases.
14. Write a Python program that accepts a comma-separated sequence of words as input and prints the
distinct words in sorted form (alphanumerically).
15. Write a Python function to create an HTML string with tags around the word(s).
17. Write a Python function to get a string made of 4 copies of the last two characters of a specified string
(length must be at least 2).
18. Write a Python function to get a string made of the first three characters of a specified string. If the
length of the string is less than 3, return the original string.
19. Write a Python program to get the last part of a string before a specified character.
22.Write a Python program to sort a string lexicographically.(do both using library and without library)
23. Write a Python program to remove a newline in Python.(do both using library and without library)
24. Write a Python program to check whether a string starts with specified characters.(do both using
library and without library)
Note : In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or
Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of
substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of
positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would
become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.
Sample input
para = """Nature around us is a gift. We need to handle it wisely.
Nature's gifts are for everyone and many generations. Every generation,
width=50
output:(-> and <- arrow are shown to highlight start and end of line)
27. Write a Python program to remove existing indentation from all of the lines in a given text.
28. Write a Python program to add prefix text to all of the lines in a string.(do both using library and
without library)
Example:
29. Write a Python program to set the indentation (4 spaces) of the first line, from input string containing
a paragraph of text.
Sample input
Nature's gifts are for everyone and many generations. Every generation,
Output:
""" Nature around us is a gift. We need to handle it wisely.
Nature's gifts are for everyone and many generations. Every generation,
30. Write a Python program to print the following numbers up to 2 decimal places.
a. 123.45678
b. 0.0000004
c. 123
d. 123456789.9
31. Write a Python program to print the following numbers up to 2 decimal places with a sign.
a. 123.45678
b. -0.0000004
c. 123
d. -123456789.9
32. Write a Python program to print the following positive and negative numbers with no decimal places.
a. 123.45678
b. -0.0000004
c. 123
d. -123456789.9
33. Write a Python program to print the following integers with zeros to the left of the specified width. If
integer with greater width, then display error message.
a. Use format
b. write your logic to perform this task
Example : width =5
input output
345 00345
56789 56789
0 00000
123456 Error
34. Write a Python program to print the following integers with '*' to the right of the specified width. If
integer with greater width, then display error message.
a. Use format
Example : width =5
input output
345 345**
56789 56789
0 0****
123456 Error
35. Write a Python program to display a number with a comma separator. After every thousand there
should be comma added. (do both using library and without library)
Ex.
input output
0 0
-123456 -123,456
123456789 123,456,789
9876 9,876
78 78
36. Write a Python program to format a number with a percentage.(do both using library and without
library)
37. Write a Python program to display a number in left, right, and center aligned with a width of 10.
38. Write a Python program to count occurrences of a substring in a string.(do both using library and
without library)
39. Write a Python program to reverse a string.(do both using library,using slice operator and without
library)
40. Write a Python program to reverse all words in a string.(do both using slice and without library)
Ex
41. Write a Python program to strip a set of characters from a string.(do both using library and without
library)
42. Write a Python program to count repeated characters in a string.(do both using set and without set, in
both case store in dictionary)
Expected output :
o4
e3
u2
h2
r2
t2
43. Write a Python program to print the square and cube symbols in the area of a rectangle and the
volume of a cylinder.
Sample output:
Expected output:
-------------------------
45. Write a Python program to check whether a string contains all letters of the alphabet[a-z].
46. Write a Python program to convert a given string into a list of words.
Sample input:
Sample Output:
50. Write a Python program to split a string on the last occurrence of the delimiter.
51. Write a Python program to find the first non-repeating character in a given string.
52. Write a Python program to print all permutations with a given repetition number of characters of a
given string.
53. Write a Python program to find the first repeated character in a given string.
54. Write a Python program to find the first repeated character in a given string where the index of the
first occurrence is smallest. Return the smallest index.
55.Write a Python program to find the first repeated word in a given string.
56. Write a Python program to find the second most repeated word in a given string.
58. Write a Python program to move spaces to the front of a given string.
59. Write a Python program to find the maximum number of characters in a given string.
60. Write a Python program to capitalize the first and last letters of each word in a given string.
61. Write a Python program to remove duplicate characters from a given string.
62. Write a Python program to compute the sum of the digits in a given string.
64. Write a Python program to find the maximum length of consecutive 0's in a given binary string.
65. Write a Python program to find all the common characters in lexicographical order from two given
lower case strings. If there are no similar letters print "No common characters".
66. Write a Python program to make two given strings (lower case, may or may not be of the same length)
anagrams without removing any characters from any of the strings.
67. Write a Python program to remove all consecutive duplicates of a given string.
68. Write a Python program to generate two strings from a given string. For the first string, use the
characters that occur only once, and for the second, use the characters that occur multiple times in the
said string.
69. Write a Python program to find the longest common sub-string from two given strings.
70. Write a Python program that concatenates uncommon characters from two strings.
71. Write a Python program to move all spaces to the front of a given string in a single traversal.
72. Write a Python program to remove all characters except a specified character from a given string.
Original string
Python Exercises
Original string
gg
Original string
exercises
eee
73. Write a Python program to count Uppercase, Lowercase, special characters and numeric values in a
given string.
74. Write a Python program to find the minimum window in a given string that will contain all the
characters of another given string.
Example 1
str2 = "OSU"
75. Write a Python program to find the smallest window that contains all characters in a given string.
Example 1
Input : "CDAC"
76. Write a Python program to count the number of substrings from a given string of lowercase alphabets
with exactly k distinct (given) characters.
input:
str = "CDAC"
k=2
Output: 3
Explainationation, there are 3 substrings of 2 distinct characters in given input "CD", "DA", "AC"
input:
str = "CCEE"
k=2
Output: 1
Explainationation,
total substrings of length 2 in given input are CC, CE and EE
77. Write a Python program to count the number of non-empty substrings of a given string.
78. Write a Python program to count characters at the same position in a given string (lower and
uppercase characters) as in the English alphabet.
79. Write a Python program to find the smallest and largest words in a given string.
80. Write a Python program to count the number of substrings with the same first and last characters in a
given string.
81. Write a Python program to determine the index of a given string at which a certain substring starts. If
the substring is not found in the given string return 'Not found'.
82. Write a Python program to wrap a given string into a paragraph with a given width.
Sample Output:
Result:
The quick
brown fox.
83. Write a Python program to print four integer values - decimal, octal, hexadecimal (capitalized), binary
- in a single line.
Sample Output:
Input an integer: 25
25 31 19 11001
Sample Output:
pYTHON eXERCISES
jAVA
nUMpY
Sample Output:
Original Bytearray :
Hexadecimal string:
6f0c2d436d
86. Write a Python program to delete all occurrences of a specified character in a given string.
Sample Output:
Original string:
Modified string:
87. Write a Python program to find the common values that appear in two given strings.
Sample Output:
Original strings:
Python3
Python2.7
Python
88. 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:
['Valid string.']
89. Write a Python program to remove unwanted characters from a given string.
Sample Output:
Python Exercises
ABCD
90. Write a Python program to remove duplicate words from a given string.
Sample Output:
Original String:
91. Write a Python program to convert a given heterogeneous list of scalars into a string.
Sample Output:
Original list:
Red,100,-50,green,w,3,r,12.12,False
92. Write a Python program to find string similarity between two given strings.
Sample Output:
Original string:
Python Exercises
Python Exercises
1.0
Original string:
Python Exercises
Python Exercise
0.967741935483871
Original string:
Python Exercises
Python Ex.
0.6923076923076923
Original string:
Python Exercises
Python
0.5454545454545454
Original string:
Java Exercises
Python
0.0
Sample Output:
Original string: red 12 black 45 green
94. Write a Python program to convert a hexadecimal color code to a tuple of integers corresponding to
its RGB components.
Sample Output:
(255, 165, 1)
(0, 0, 0)
(255, 0, 0)
(0, 0, 128)
95. Write a Python program to convert the values of RGB components to a hexadecimal color code.
Sample Output:
FFA501
FFFFFF
000000
000080
C0C0C0
Sample Output:
javascript
fooBar
fooBar
foo.Bar
fooBar
foobar
fooBar
Sample Output:
java_script
foo_bar
foo_bar
foo.bar
foo_bar
foo_bar
foo_bar
98. Write a Python program to decapitalize the first letter of a given string.
Sample Output:
java Script
python
99. Write a Python program to split a multi-line string into a list of lines.
Sample Output:
is a
multiline
string.
100. Write a Python program to check whether any word in a given string contains duplicate characters or
not. Return True or False.
Sample Output:
Original text:
Check whether any word in the said sting contains duplicate characrters or not!
False
Original text:
Python Exercise.
Check whether any word in the said sting contains duplicate characrters or not!
False
Original text:
Check whether any word in the said sting contains duplicate characrters or not!
True
101. Write a Python program to add two strings as if they were numbers (positive integer values). Return
a message if the numbers are strings.
Sample Output:
42
Error in input!
Error in input!
Sample Output:
Original text:
103. Write a Python program to replace each character of a word of length five and more with a hash
character (#).
Sample Output:
Original string: Count the lowercase letters in the said list of words:
Replace words (length five or more) with hash characters in the said string:
Replace words (length five or more) with hash characters in the said string:
104. Write a Python program that capitalizes the first letter and lowercases the remaining letters in a
given string.
Sample Data:
Sample Data:
106. Write a Python program to remove repeated consecutive characters and replace them with single
letters and print a updated string.
Sample Data:
107. Write a Python program that takes two strings. Count the number of times each string contains the
same three letters at the same index.
Sample Data:
108. Write a Python program that takes a string and returns # on both sides of each element, which are
not vowels.
Sample Data:
109. Write a Python program that counts the number of leap years within the range of years. Ranges of
years should be accepted as strings.
Sample Data:
("1981-1991") -> 2
("2000-2020") -> 6
110. Write a Python program to insert space before every capital letter appears in a given word.
Sample Data:
111. Write a Python program that takes a string and replaces all the characters with their respective
numbers.
Sample Data:
112. Write a Python program to calculate the sum of two numbers given as strings. Return the result in
the same string representation.
Sample Data:
( "234242342341", "2432342342") -> "236674684683"
113. Write a Python program that returns a string sorted alphabetically by the first character of a given
string of words.
Sample Data:
("Red Green Black White Pink") -> "Black Green Pink Red White"
("Calculate the sum of two said numbers given as strings.") -> ("Calculate as given numbers of sum said
strings. the two")
("The quick brown fox jumps over the lazy dog.") -> ("The brown dog. fox jumps lazy over quick the")