Does a string contain every letter?

Python Practice › String Programs › Program 63

Does a string contain every letter?

EasyString Programs · 63 of 106

Write a Python program to check whether a string contains all letters of the alphabet (a pangram).

Input
"abcdgjksoug", "The quick brown fox jumps over the lazy dog"
Output
False
True

Approach

  1. Compare the set of letters with string.ascii_lowercase.

Solution

Python
import string for text in ["abcdgjksoug", "The quick brown fox jumps over the lazy dog"]:    print(set(string.ascii_lowercase) <= set(text.lower()))
Output
False
True

Learn the concept

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

More string programs

← All 106 String Programs

Strip spaces

Python Practice › String Programs › Program 62

Strip spaces

EasyString Programs · 62 of 106

Write a Python program to remove leading and trailing spaces.

Input
"    sqaltoolspythonfun     "
Output
'sqaltoolspythonfun' 'sqaltoolspythonfun     ' '    sqaltoolspythonfun'

Approach

  1. strip(); lstrip() / rstrip() for one side.

Solution

Python
text = "    sqaltoolspythonfun     "print(repr(text.strip()), repr(text.lstrip()), repr(text.rstrip()))
Output
'sqaltoolspythonfun' 'sqaltoolspythonfun     ' '    sqaltoolspythonfun'

Learn the concept

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

More string programs

← All 106 String Programs

Index of a character

Python Practice › String Programs › Program 61

Index of a character

EasyString Programs · 61 of 106

Write a Python program to print the index of a character in a string.

Input
"Sqatools", "q"
Output
The index of q is 1

Approach

  1. find() returns the first index (or -1).

Solution

Python
text = "Sqatools"ch = "q"print(f"The index of {ch} is {text.find(ch)}")
Output
The index of q is 1

Learn the concept

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

More string programs

← All 106 String Programs

Frequency of every substring

Python Practice › String Programs › Program 60

Frequency of every substring

MediumString Programs · 60 of 106

Write a Python program to count every substring of a string.

Input
"abab"
Output
{'a': 2, 'ab': 2, 'aba': 1, 'abab': 1, 'b': 2, 'ba': 1, 'bab': 1}

Approach

  1. Two nested loops pick every start and end position.

Solution

Python
text = "abab"freq = {}for i in range(len(text)):    for j in range(i + 1, len(text) + 1):        sub = text[i:j]        freq[sub] = freq.get(sub, 0) + 1print(freq)
Output
{'a': 2, 'ab': 2, 'aba': 1, 'abab': 1, 'b': 2, 'ba': 1, 'bab': 1}

Line by line

Lines 3-5

text[i:j] visits every substring once: “a”, “ab”, “aba”, “abab”, “b”, …

Learn the concept

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

More string programs

← All 106 String Programs

Is a substring present?

Python Practice › String Programs › Program 59

Is a substring present?

EasyString Programs · 59 of 106

Write a Python program to check whether a substring is present in a string.

Input
"I live in Pune", "I live "
Output
Yes

Approach

  1. Use in.

Solution

Python
text = "I live in Pune"print("Yes" if "I live " in text else "No")
Output
Yes

Learn the concept

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

More string programs

← All 106 String Programs

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