Swap case
Write a Python program to swap upper and lower case.
"Learning Python"
lEARNING pYTHON
Approach
swapcase().
Solution
print("Learning Python".swapcase())lEARNING pYTHON
Learn the concept
New to this topic? Read the lesson, then come back.
Online Learning Platform
Python practice problems help beginners to become experts in a python programming language with a small amount of practice effort.
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.
Write a Python program to accept a string only if it contains vowels only.
"python", "aaieou"
python not accepted aaieou accepted
all(ch in "aeiou" for ch in text).for text in ["python", "aaieou"]: print(text, "accepted" if text and all(ch in "aeiou" for ch in text.lower()) else "not accepted")python not accepted aaieou accepted
text and ... rejects the empty string, for which all() would return True.
New to this topic? Read the lesson, then come back.
Write a Python program to get the length of a string, not counting spaces.
"sqatools is best for learning python"
31
len().text = "sqatools is best for learning python"print(len(text.replace(" ", "")))31
New to this topic? Read the lesson, then come back.