Add "ing" to a string
Write a Python program to add “ing” at the end of a string.
"xyz"
xyzing
Approach
- Concatenate.
Solution
print("xyz" + "ing")xyzing
Learn the concept
New to this topic? Read the lesson, then come back.
Online Learning Platform
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.
Write a Python program to list all permutations of a string with a built-in function.
"CDE"
['CDE', 'CED', 'DCE', 'DEC', 'ECD', 'EDC']
itertools.permutations and join each tuple.from itertools import permutationsprint(["".join(p) for p in permutations("CDE")])['CDE', 'CED', 'DCE', 'DEC', 'ECD', 'EDC']
New to this topic? Read the lesson, then come back.
Write a Python program to split a string and join it with “-“.
"Sqatools is best"
Sqatools-is-best
"-".join(text.split()).print("-".join("Sqatools is best".split()))Sqatools-is-best
New to this topic? Read the lesson, then come back.
Write a Python program to insert a space before every capital letter.
"SqaTools pyThon"
' Sqa Tools py Thon'
re.sub to put a space before each uppercase letter.import reprint(repr(re.sub(r"([A-Z])", r" \1", "SqaTools pyThon")))' Sqa Tools py Thon'
\1 puts the captured capital back after the inserted space.
New to this topic? Read the lesson, then come back.
Write a Python program to convert the second half of a string to uppercase.
"banana"
banANA
text = "banana"mid = len(text) // 2print(text[:mid] + text[mid:].upper())banANA
New to this topic? Read the lesson, then come back.
Write a Python program to count the leap years in a range given as a string like “1981-2001”.
"1981-2001"
Total leap years: 5 [1984, 1988, 1992, 1996, 2000]
calendar.isleap.import calendar text = "1981-2001"start, end = map(int, text.split("-"))leaps = [y for y in range(start, end + 1) if calendar.isleap(y)]print("Total leap years:", len(leaps), leaps)Total leap years: 5 [1984, 1988, 1992, 1996, 2000]
map(int, ...) converts both parts.
New to this topic? Read the lesson, then come back.