Reverse the words

Python Practice › String Programs › Program 98

Reverse the words

EasyString Programs · 98 of 106

Write a Python program to reverse the order of words.

Input
"string problems"
Output
problems string

Approach

  1. " ".join(reversed(text.split())).

Solution

Python
print(" ".join(reversed("string problems".split())))
Output
problems string

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Add “ly” after “ing”

Python Practice › String Programs › Program 97

Add "ly" after "ing"

EasyString Programs · 97 of 106

Write a Python program to add “ly” if a string ends with “ing”, otherwise add “ing”.

Input
"winning", "play"
Output
winningly
playing

Approach

  1. endswith("ing") decides which suffix to add.

Solution

Python
for word in ["winning", "play"]:    print(word + ("ly" if word.endswith("ing") else "ing"))
Output
winningly
playing

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Add “ing” to a string

Python Practice › String Programs › Program 96

Add "ing" to a string

EasyString Programs · 96 of 106

Write a Python program to add “ing” at the end of a string.

Input
"xyz"
Output
xyzing

Approach

  1. Concatenate.

Solution

Python
print("xyz" + "ing")
Output
xyzing

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Is a string binary?

Python Practice › String Programs › Program 95

Is a string binary?

EasyString Programs · 95 of 106

Write a Python program to check whether a string contains only 0 and 1.

Input
"01011100", "0120"
Output
01011100 yes
0120 no

Approach

  1. The set of characters must be a subset of {“0”, “1”}.

Solution

Python
for s in ["01011100", "0120"]:    print(s, "yes" if s and set(s) <= {"0", "1"} else "no")
Output
01011100 yes
0120 no

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Remove the kth character

Python Practice › String Programs › Program 94

Remove the kth character

EasyString Programs · 94 of 106

Write a Python program to remove the character at index k.

Input
k = 2, "sqatools"
Output
sqtools

Approach

  1. Join the parts before and after index k.

Solution

Python
text, k = "sqatools", 2print(text[:k] + text[k + 1:])
Output
sqtools

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Accept only vowel strings

Python Practice › String Programs › Program 93

Accept only vowel strings

EasyString Programs · 93 of 106

Write a Python program to accept a string only if it contains vowels only.

Input
"python", "aaieou"
Output
python not accepted
aaieou accepted

Approach

  1. all(ch in "aeiou" for ch in text).

Solution

Python
for text in ["python", "aaieou"]:    print(text, "accepted" if text and all(ch in "aeiou" for ch in text.lower()) else "not accepted")
Output
python not accepted
aaieou accepted

Line by line

Line 2

text and ... rejects the empty string, for which all() would return True.

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Length without spaces

Python Practice › String Programs › Program 92

Length without spaces

EasyString Programs · 92 of 106

Write a Python program to get the length of a string, not counting spaces.

Input
"sqatools is best for learning python"
Output
31

Approach

  1. Remove spaces, then use len().

Solution

Python
text = "sqatools is best for learning python"print(len(text.replace(" ", "")))
Output
31

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Permutations of a string

Python Practice › String Programs › Program 91

Permutations of a string

EasyString Programs · 91 of 106

Write a Python program to list all permutations of a string with a built-in function.

Input
"CDE"
Output
['CDE', 'CED', 'DCE', 'DEC', 'ECD', 'EDC']

Approach

  1. itertools.permutations and join each tuple.

Solution

Python
from itertools import permutationsprint(["".join(p) for p in permutations("CDE")])
Output
['CDE', 'CED', 'DCE', 'DEC', 'ECD', 'EDC']

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Join words with hyphens

Python Practice › String Programs › Program 90

Join words with hyphens

EasyString Programs · 90 of 106

Write a Python program to split a string and join it with “-“.

Input
"Sqatools is best"
Output
Sqatools-is-best

Approach

  1. "-".join(text.split()).

Solution

Python
print("-".join("Sqatools is best".split()))
Output
Sqatools-is-best

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Space before each capital letter

Python Practice › String Programs › Program 88

Space before each capital letter

EasyString Programs · 88 of 106

Write a Python program to insert a space before every capital letter.

Input
"SqaTools pyThon"
Output
' Sqa Tools py Thon'

Approach

  1. Use re.sub to put a space before each uppercase letter.

Solution

Python
import reprint(repr(re.sub(r"([A-Z])", r" \1", "SqaTools pyThon")))
Output
' Sqa Tools py Thon'

Line by line

Line 2

\1 puts the captured capital back after the inserted space.

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs