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

Extract numbers as integers

Python Practice › String Programs › Program 83

Extract numbers as integers

EasyString Programs · 83 of 106

Write a Python program to extract the numbers from a string.

Input
"python 456 self learning 89"
Output
[456, 89]

Approach

  1. Keep words that are digits and convert them.

Solution

Python
text = "python 456 self learning 89"print([int(w) for w in text.split() if w.isdigit()])
Output
[456, 89]

Learn the concept

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

More string programs

← All 106 String Programs

Similarity of two strings

Python Practice › String Programs › Program 82

Similarity of two strings

MediumString Programs · 82 of 106

Write a Python program to measure how similar two strings are.

Input
"Learning is fun in Sqatools", "Sqatools Online Learning Platform"
Output
Similarity: 0.4
Word overlap (Jaccard): 0.29

Approach

  1. difflib.SequenceMatcher returns a ratio from 0 (different) to 1 (identical).

Solution

Python
from difflib import SequenceMatcher s1 = "Learning is fun in Sqatools"s2 = "Sqatools Online Learning Platform"ratio = SequenceMatcher(None, s1, s2).ratio()print("Similarity:", round(ratio, 2)) w1, w2 = set(s1.lower().split()), set(s2.lower().split())print("Word overlap (Jaccard):", round(len(w1 & w2) / len(w1 | w2), 2))
Output
Similarity: 0.4
Word overlap (Jaccard): 0.29

Line by line

Line 5

The ratio compares matching blocks of characters.

Lines 8-9

A second measure compares shared words over all distinct words.

Note

Different similarity measures give different numbers, so state which one you use.

Learn the concept

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

More string programs

← All 106 String Programs