Random binary string

Python Practice › String Programs › Program 58

Random binary string

EasyString Programs · 58 of 106

Write a Python program to generate a random binary string of a given length.

Input
8
Output
00110011

Approach

  1. Pick “0” or “1” for each position.

Solution

Python
import random random.seed(3)n = 8print("".join(random.choice("01") for _ in range(n)))
Output
00110011

Line by line

Line 3

The seed makes the output repeatable; remove it for a new string each run.

Learn the concept

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

More string programs

← All 106 String Programs

Sort the characters of a string

Python Practice › String Programs › Program 57

Sort the characters of a string

EasyString Programs · 57 of 106

Write a Python program to sort the characters of a string.

Input
"xyabkmp"
Output
abkmpxy

Approach

  1. sorted() returns a list of characters; join it.

Solution

Python
print("".join(sorted("xyabkmp")))
Output
abkmpxy

Learn the concept

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

More string programs

← All 106 String Programs

Is one string a subset of another?

Python Practice › String Programs › Program 56

Is one string a subset of another?

EasyString Programs · 56 of 106

Write a Python program to check whether all characters of one string appear in another.

Input
"iamlearningpythonatsqatools", "pystlmi"
Output
True

Approach

  1. Compare character sets with <=.

Solution

Python
big = "iamlearningpythonatsqatools"small = "pystlmi"print(set(small) <= set(big))
Output
True

Line by line

Line 3

<= on sets means “is a subset of”.

Learn the concept

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

More string programs

← All 106 String Programs

Find duplicate characters

Python Practice › String Programs › Program 55

Find duplicate characters

EasyString Programs · 55 of 106

Write a Python program to find the characters that appear more than once (ignoring spaces).

Input
"hello world"
Output
lo

Approach

  1. Count characters and keep those with a count above 1.

Solution

Python
from collections import Counter text = "hello world"counts = Counter(text.replace(" ", ""))print("".join(ch for ch, n in counts.items() if n > 1))
Output
lo

Learn the concept

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

More string programs

← All 106 String Programs

Remove punctuation

Python Practice › String Programs › Program 54

Remove punctuation

EasyString Programs · 54 of 106

Write a Python program to remove punctuation from a string.

Input
"Sqatools : is best, for python"
Output
Sqatools is best for python

Approach

  1. Delete every character in string.punctuation, then tidy extra spaces.

Solution

Python
import string text = "Sqatools : is best, for python"clean = text.translate(str.maketrans("", "", string.punctuation))print(" ".join(clean.split()))
Output
Sqatools is best for python

Line by line

Line 4

The third argument of maketrans lists characters to delete.

Line 5

split() and join collapse the double space left by the removed colon.

Learn the concept

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

More string programs

← All 106 String Programs

Remove blank strings from a list

Python Practice › String Programs › Program 53

Remove blank strings from a list

EasyString Programs · 53 of 106

Write a Python program to remove blank strings from a list of strings.

Input
["Python", " ", " ", "sqatools"]
Output
['Python', 'sqatools']

Approach

  1. Keep items whose stripped text is not empty.

Solution

Python
items = ["Python", " ", " ", "sqatools"]print([s for s in items if s.strip()])
Output
['Python', 'sqatools']

Line by line

Line 2

" ".strip() is "", which is falsy, so blank items are dropped.

Learn the concept

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

More string programs

← All 106 String Programs

Replace several characters at once

Python Practice › String Programs › Program 52

Replace several characters at once

EasyString Programs · 52 of 106

Write a Python program to replace a with 1, t with 2 and o with 3 in one step.

Input
"Sqatool python"
Output
Sq1233l py2h3n

Approach

  1. Build a translation table with str.maketrans and apply translate.

Solution

Python
text = "Sqatool python"table = str.maketrans({"a": "1", "t": "2", "o": "3"})print(text.translate(table))
Output
Sq1233l py2h3n

Line by line

Line 2

maketrans maps each character to its replacement.

Line 3

translate replaces all of them in a single pass.

Learn the concept

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

More string programs

← All 106 String Programs

Replace several words at once

Python Practice › String Programs › Program 51

Replace several words at once

EasyString Programs · 51 of 106

Write a Python program to replace several words using a dictionary of replacements.

Input
"I'm learning python at Sqatools"; python → SQA, sqatools → TOOLS
Output
I'm learning SQA at TOOLS

Approach

  1. Use a regex that matches any of the words, ignoring case, and look up each replacement.

Solution

Python
import re text = "I'm learning python at Sqatools"replacements = {"python": "SQA", "sqatools": "TOOLS"}pattern = r"\b(" + "|".join(replacements) + r")\b"print(re.sub(pattern, lambda m: replacements[m.group(0).lower()], text, flags=re.IGNORECASE))
Output
I'm learning SQA at TOOLS

Line by line

Line 5

Builds the pattern \b(python|sqatools)\b.

Line 6

For every match the lambda returns the replacement, looked up in lower case.

Learn the concept

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

More string programs

← All 106 String Programs

Split a string on vowels

Python Practice › String Programs › Program 50

Split a string on vowels

EasyString Programs · 50 of 106

Write a Python program to split a string wherever a vowel appears.

Input
"qwerty"
Output
['qw', 'rty']
pr gr mm ng

Approach

  1. re.split("[aeiou]", text) removes the vowels and splits there.

Solution

Python
import reprint(re.split(r"[aeiouAEIOU]", "qwerty"))print(" ".join(re.split(r"[aeiou]", "programming")))
Output
['qw', 'rty']
pr gr mm ng

Learn the concept

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

More string programs

← All 106 String Programs

Mirror image of a string

Python Practice › String Programs › Program 49

Mirror image of a string

EasyString Programs · 49 of 106

Write a Python program to print the mirror image (reverse) of a string.

Input
"Python"
Output
nohtyP

Approach

  1. [::-1].

Solution

Python
print("Python"[::-1])
Output
nohtyP

Learn the concept

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

More string programs

← All 106 String Programs