Keep only one given character

Python Practice › String Programs › Program 78

Keep only one given character

EasyString Programs · 78 of 106

Write a Python program to remove all characters except a given one.

Input
"Sqatools python", keep "S"
Output
S

Approach

  1. Keep characters equal to the given one.

Solution

Python
text = "Sqatools python"keep = "S"print("".join(ch for ch in text if ch == keep))
Output
S

Learn the concept

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

More string programs

← All 106 String Programs

Uncommon characters of two strings

Python Practice › String Programs › Program 77

Uncommon characters of two strings

EasyString Programs · 77 of 106

Write a Python program to build a string from the characters that appear in only one of two strings.

Input
"abcdefg", "xyzabcd"
Output
efgxyz

Approach

  1. Keep characters of each string that are not in the other.

Solution

Python
s1, s2 = "abcdefg", "xyzabcd"result = [ch for ch in s1 if ch not in s2] + [ch for ch in s2 if ch not in s1]print("".join(result))
Output
efgxyz

Learn the concept

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

More string programs

← All 106 String Programs

Characters that occur more than once

Python Practice › String Programs › Program 76

Characters that occur more than once

EasyString Programs · 76 of 106

Write a Python program to build a string of the characters that occur more than once.

Input
"aabbcceffgh"
Output
abcf

Approach

  1. Count, then keep characters with a count above 1, in first-seen order.

Solution

Python
from collections import Countertext = "aabbcceffgh"counts = Counter(text)print("".join(ch for ch in counts if counts[ch] > 1))
Output
abcf

Learn the concept

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

More string programs

← All 106 String Programs

Remove consecutive duplicates

Python Practice › String Programs › Program 75

Remove consecutive duplicates

EasyString Programs · 75 of 106

Write a Python program to collapse consecutive duplicate characters into one.

Input
"xxxxyy"
Output
xyx

Approach

  1. Add a character only if it differs from the last one added.

Solution

Python
text = "xxxxyyxx"result = ""for ch in text:    if not result or ch != result[-1]:        result += chprint(result)
Output
xyx

Line by line

Line 4

not result handles the first character.

Note

Only consecutive repeats are removed, so “xxxxyyxx” becomes “xyx”.

Learn the concept

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

More string programs

← All 106 String Programs

Longest run of zeros

Python Practice › String Programs › Program 74

Longest run of zeros

EasyString Programs · 74 of 106

Write a Python program to find the longest run of consecutive 0s in a binary string.

Input
"10001100000111"
Output
5

Approach

  1. Split on “1” and take the longest piece.

Solution

Python
binary = "10001100000111"print(max(len(run) for run in binary.split("1")))
Output
5

Line by line

Line 2

Splitting on “1” leaves only the runs of zeros.

Learn the concept

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

More string programs

← All 106 String Programs

Remove leading zeros from an IP address

Python Practice › String Programs › Program 73

Remove leading zeros from an IP address

EasyString Programs · 73 of 106

Write a Python program to remove leading zeros from each part of an IP address.

Input
"289.03.02.054"
Output
289.3.2.54

Approach

  1. Split on dots, convert each part to int and back.

Solution

Python
ip = "289.03.02.054"print(".".join(str(int(part)) for part in ip.split(".")))
Output
289.3.2.54

Line by line

Line 2

int("054") is 54, which drops the zeros; int("0") stays 0.

Learn the concept

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

More string programs

← All 106 String Programs

Sum of digits in a string

Python Practice › String Programs › Program 72

Sum of digits in a string

EasyString Programs · 72 of 106

Write a Python program to add up the digits in a string.

Input
"12sqatools78"
Output
18

Approach

  1. Sum int(ch) for each digit character.

Solution

Python
print(sum(int(ch) for ch in "12sqatools78" if ch.isdigit()))
Output
18

Learn the concept

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

More string programs

← All 106 String Programs

Capitalise the first and last letter of each word

Python Practice › String Programs › Program 71

Capitalise the first and last letter of each word

EasyString Programs · 71 of 106

Write a Python program to capitalise the first and last letters of each word.

Input
"this is my first program"
Output
ThiS IS MY FirsT PrograM

Approach

  1. For each word: upper first + middle + upper last.

Solution

Python
def cap_ends(w):    if len(w) == 1:        return w.upper()    return w[0].upper() + w[1:-1] + w[-1].upper() print(" ".join(cap_ends(w) for w in "this is my first program".split()))
Output
ThiS IS MY FirsT PrograM

Learn the concept

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

More string programs

← All 106 String Programs

Remove all spaces

Python Practice › String Programs › Program 70

Remove all spaces

EasyString Programs · 70 of 106

Write a Python program to remove all spaces from a string.

Input
"python at sqatools"
Output
pythonatsqatools

Approach

  1. replace(" ", "").

Solution

Python
print("python at sqatools".replace(" ", ""))
Output
pythonatsqatools

Learn the concept

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

More string programs

← All 106 String Programs

Second most repeated word

Python Practice › String Programs › Program 69

Second most repeated word

EasyString Programs · 69 of 106

Write a Python program to find the second most repeated word.

Input
"ab bc ac ab bd ac nk hj ac"
Output
('ab', 2)

Approach

  1. Counter.most_common() returns words sorted by count.

Solution

Python
from collections import Counterprint(Counter("ab bc ac ab bd ac nk hj ac".split()).most_common(2)[1])
Output
('ab', 2)

Line by line

Line 2

most_common(2) gives the top two pairs; index 1 is the second.

Learn the concept

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

More string programs

← All 106 String Programs