Print a string 3 times
Write a Python program to print a string 3 times.
"sqatools"
sqatoolssqatoolssqatools
Approach
* 3.
Solution
print("sqatools" * 3)sqatoolssqatoolssqatools
Learn the concept
New to this topic? Read the lesson, then come back.
Online Learning Platform
Write a Python program to print a string 3 times.
"sqatools"
sqatoolssqatoolssqatools
* 3.print("sqatools" * 3)sqatoolssqatoolssqatools
New to this topic? Read the lesson, then come back.
Write a Python program to replace each group of repeated characters with a single character.
"aabbccdd"
abcd
dict.fromkeys keeps one of each in order.print("".join(dict.fromkeys("aabbccdd")))abcd
The result keeps the original order, “abcd”. The original sample’s “cabd” came from using a set, whose order is not defined.
New to this topic? Read the lesson, then come back.
Write a Python program to swap upper and lower case.
"Learning Python"
lEARNING pYTHON
swapcase().print("Learning Python".swapcase())lEARNING pYTHON
New to this topic? Read the lesson, then come back.
Write a Python program to find the first character that repeats and the index of its first occurrence.
"sqatools"
('o', 4)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('o', 4)The first character seen twice is reported with the index where it first appeared.
The loop finds the repeat at the second “o”, so the answer is (“o”, 4): “o” repeats before “s” does.
New to this topic? Read the lesson, then come back.
Write a Python program to print the index of each character in a string.
"sqatools"
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
enumerate() gives index and character.for i, ch in enumerate("sqatools"): print(f"Index of {ch} is {i}")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
New to this topic? Read the lesson, then come back.
Write a Python program to reverse the order of words.
"string problems"
problems string
" ".join(reversed(text.split())).print(" ".join(reversed("string problems".split())))problems string
New to this topic? Read the lesson, then come back.
Write a Python program to add “ly” if a string ends with “ing”, otherwise add “ing”.
"winning", "play"
winningly playing
endswith("ing") decides which suffix to add.for word in ["winning", "play"]: print(word + ("ly" if word.endswith("ing") else "ing"))winningly playing
New to this topic? Read the lesson, then come back.
Write a Python program to add “ing” at the end of a string.
"xyz"
xyzing
print("xyz" + "ing")xyzing
New to this topic? Read the lesson, then come back.
Write a Python program to check whether a string contains only 0 and 1.
"01011100", "0120"
01011100 yes 0120 no
for s in ["01011100", "0120"]: print(s, "yes" if s and set(s) <= {"0", "1"} else "no")01011100 yes 0120 no
New to this topic? Read the lesson, then come back.
Write a Python program to remove the character at index k.
k = 2, "sqatools"
sqtools
text, k = "sqatools", 2print(text[:k] + text[k + 1:])sqtools
New to this topic? Read the lesson, then come back.