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

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