First repeated word

Python Practice › String Programs › Program 68

First repeated word

EasyString Programs · 68 of 106

Write a Python program to find the first word that repeats.

Input
"ab bc ca ab bd"
Output
ab

Approach

  1. Remember words seen; return the first one seen again.

Solution

Python
def first_repeated(text):    seen = set()    for w in text.split():        if w in seen:            return w        seen.add(w)    return None print(first_repeated("ab bc ca ab bd"))
Output
ab

Learn the concept

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

More string programs

← All 106 String Programs

Split on the last delimiter

Python Practice › String Programs › Program 67

Split on the last delimiter

EasyString Programs · 67 of 106

Write a Python program to split a string on the last occurrence of a delimiter.

Input
"l,e,a,r,n,I,n,g,p,y,t,h,o,n"
Output
['l,e,a,r,n,I,n,g,p,y,t,h,o', 'n']

Approach

  1. rsplit(",", 1) splits once, from the right.

Solution

Python
text = "l,e,a,r,n,I,n,g,p,y,t,h,o,n"print(text.rsplit(",", 1))
Output
['l,e,a,r,n,I,n,g,p,y,t,h,o', 'n']

Learn the concept

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

More string programs

← All 106 String Programs

Count and show vowels

Python Practice › String Programs › Program 66

Count and show vowels

EasyString Programs · 66 of 106

Write a Python program to count and display the vowels in a string.

Input
"welcome to Sqatools"
Output
['e', 'o', 'e', 'o', 'a', 'o', 'o'] 7

Approach

  1. Collect the vowels in a list.

Solution

Python
text = "welcome to Sqatools"vowels = [ch for ch in text if ch.lower() in "aeiou"]print(vowels, len(vowels))
Output
['e', 'o', 'e', 'o', 'a', 'o', 'o'] 7

Learn the concept

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

More string programs

← All 106 String Programs

Swap commas and dots

Python Practice › String Programs › Program 65

Swap commas and dots

EasyString Programs · 65 of 106

Write a Python program to swap commas and dots.

Input
"sqa,tools.python"
Output
sqa.tools,python

Approach

  1. translate with a two-way mapping swaps them in one pass.

Solution

Python
text = "sqa,tools.python"print(text.translate(str.maketrans(",.", ".,")))
Output
sqa.tools,python

Line by line

Line 2

maketrans(",.", ".,") maps , to . and . to , at the same time; two replace calls would undo each other.

Learn the concept

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

More string programs

← All 106 String Programs

String to a list of words

Python Practice › String Programs › Program 64

String to a list of words

EasyString Programs · 64 of 106

Write a Python program to convert a string into a list of words.

Input
"learning python is fun"
Output
['learning', 'python', 'is', 'fun']

Approach

  1. split().

Solution

Python
print("learning python is fun".split())
Output
['learning', 'python', 'is', 'fun']

Learn the concept

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

More string programs

← All 106 String Programs

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