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

Permutations of a string

Python Practice › String Programs › Program 91

Permutations of a string

EasyString Programs · 91 of 106

Write a Python program to list all permutations of a string with a built-in function.

Input
"CDE"
Output
['CDE', 'CED', 'DCE', 'DEC', 'ECD', 'EDC']

Approach

  1. itertools.permutations and join each tuple.

Solution

Python
from itertools import permutationsprint(["".join(p) for p in permutations("CDE")])
Output
['CDE', 'CED', 'DCE', 'DEC', 'ECD', 'EDC']

Learn the concept

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

More string programs

← All 106 String Programs

Join words with hyphens

Python Practice › String Programs › Program 90

Join words with hyphens

EasyString Programs · 90 of 106

Write a Python program to split a string and join it with “-“.

Input
"Sqatools is best"
Output
Sqatools-is-best

Approach

  1. "-".join(text.split()).

Solution

Python
print("-".join("Sqatools is best".split()))
Output
Sqatools-is-best

Learn the concept

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

More string programs

← All 106 String Programs

Space before each capital letter

Python Practice › String Programs › Program 88

Space before each capital letter

EasyString Programs · 88 of 106

Write a Python program to insert a space before every capital letter.

Input
"SqaTools pyThon"
Output
' Sqa Tools py Thon'

Approach

  1. Use re.sub to put a space before each uppercase letter.

Solution

Python
import reprint(repr(re.sub(r"([A-Z])", r" \1", "SqaTools pyThon")))
Output
' Sqa Tools py Thon'

Line by line

Line 2

\1 puts the captured capital back after the inserted space.

Learn the concept

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

More string programs

← All 106 String Programs

Uppercase the second half

Python Practice › String Programs › Program 89

Uppercase the second half

EasyString Programs · 89 of 106

Write a Python program to convert the second half of a string to uppercase.

Input
"banana"
Output
banANA

Approach

  1. Split at the midpoint.

Solution

Python
text = "banana"mid = len(text) // 2print(text[:mid] + text[mid:].upper())
Output
banANA

Learn the concept

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

More string programs

← All 106 String Programs

Leap years in a range given as text

Python Practice › String Programs › Program 87

Leap years in a range given as text

EasyString Programs · 87 of 106

Write a Python program to count the leap years in a range given as a string like “1981-2001”.

Input
"1981-2001"
Output
Total leap years: 5 [1984, 1988, 1992, 1996, 2000]

Approach

  1. Split the text, convert both years, count with calendar.isleap.

Solution

Python
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)
Output
Total leap years: 5 [1984, 1988, 1992, 1996, 2000]

Line by line

Line 4

map(int, ...) converts both parts.

Learn the concept

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

More string programs

← All 106 String Programs

Name from an email address

Python Practice › String Programs › Program 86

Name from an email address

EasyString Programs · 86 of 106

Write a Python program to extract the name part of an email address (without trailing digits).

Input
"student1@gmail.com"
Output
student

Approach

  1. Take the part before “@”, then strip digits from the end.

Solution

Python
email = "student1@gmail.com"name = email.split("@")[0].rstrip("0123456789")print(name)
Output
student

Learn the concept

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

More string programs

← All 106 String Programs

Add two numeric strings

Python Practice › String Programs › Program 85

Add two numeric strings

EasyString Programs · 85 of 106

Write a Python program to add two strings as numbers and return a string.

Input
"3", "7"
Output
'10' | without conversion: 37

Approach

  1. Convert to int, add, convert back.

Solution

Python
a, b = "3", "7"print(repr(str(int(a) + int(b))), "| without conversion:", a + b)
Output
'10' | without conversion: 37

Learn the concept

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

More string programs

← All 106 String Programs

Split a multi-line string into lines

Python Practice › String Programs › Program 84

Split a multi-line string into lines

EasyString Programs · 84 of 106

Write a Python program to split a multi-line string into a list of lines.

Input
a three-line string
Output
['This string Contains', 'Multiple', 'Lines']

Approach

  1. splitlines().

Solution

Python
text = """This string ContainsMultipleLines"""print(text.splitlines())
Output
['This string Contains', 'Multiple', 'Lines']

Learn the concept

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

More string programs

← All 106 String Programs