Swap case

Python Practice › String Programs › Program 101

Swap case

EasyString Programs · 101 of 106

Write a Python program to swap upper and lower case.

Input
"Learning Python"
Output
lEARNING pYTHON

Approach

  1. swapcase().

Solution

Python
print("Learning Python".swapcase())
Output
lEARNING pYTHON

Learn the concept

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

More string programs

← All 106 String Programs

First repeated character and its index

Python Practice › String Programs › Program 100

First repeated character and its index

EasyString Programs · 100 of 106

Write a Python program to find the first character that repeats and the index of its first occurrence.

Input
"sqatools"
Output
('o', 4)

Approach

  1. Remember the first index of each character; stop at the first repeat.

Solution

Python
text = "sqatools"first_seen = {}for i, ch in enumerate(text):    if ch in first_seen:        print((ch, first_seen[ch]))        break    first_seen[ch] = i
Output
('o', 4)

Line by line

Lines 4-6

The first character seen twice is reported with the index where it first appeared.

Note

The loop finds the repeat at the second “o”, so the answer is (“o”, 4): “o” repeats before “s” does.

Learn the concept

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

More string programs

← All 106 String Programs

Index of each character

Python Practice › String Programs › Program 99

Index of each character

EasyString Programs · 99 of 106

Write a Python program to print the index of each character in a string.

Input
"sqatools"
Output
Index of s is 0
Index of q is 1
Index of a is 2
Index of t is 3
Index of o is 4
Index of o is 5
Index of l is 6
Index of s is 7

Approach

  1. enumerate() gives index and character.

Solution

Python
for i, ch in enumerate("sqatools"):    print(f"Index of {ch} is {i}")
Output
Index of s is 0
Index of q is 1
Index of a is 2
Index of t is 3
Index of o is 4
Index of o is 5
Index of l is 6
Index of s is 7

Learn the concept

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

More string programs

← All 106 String Programs

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