Print a string 3 times

Python Practice › String Programs › Program 103

Print a string 3 times

EasyString Programs · 103 of 106

Write a Python program to print a string 3 times.

Input
"sqatools"
Output
sqatoolssqatoolssqatools

Approach

  1. * 3.

Solution

Python
print("sqatools" * 3)
Output
sqatoolssqatoolssqatools

Learn the concept

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

More string programs

← All 106 String Programs

Collapse repeated characters

Python Practice › String Programs › Program 102

Collapse repeated characters

EasyString Programs · 102 of 106

Write a Python program to replace each group of repeated characters with a single character.

Input
"aabbccdd"
Output
abcd

Approach

  1. dict.fromkeys keeps one of each in order.

Solution

Python
print("".join(dict.fromkeys("aabbccdd")))
Output
abcd
Note

The result keeps the original order, “abcd”. The original sample’s “cabd” came from using a set, whose order is not defined.

Learn the concept

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

More string programs

← All 106 String Programs

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