Does a string have special characters?

Python Practice › String Programs › Program 36

Does a string have special characters?

EasyString Programs · 36 of 106

Write a Python program to check whether a string contains special characters.

Input
"python$$#sqatools", "python"
Output
python$$#sqatools -> has special characters ['$', '$', '#']
python -> no special characters []

Approach

  1. A special character is not alphanumeric and not a space.

Solution

Python
for text in ["python$$#sqatools", "python"]:    special = [ch for ch in text if not ch.isalnum() and not ch.isspace()]    print(text, "->", "has special characters" if special else "no special characters", special)
Output
python$$#sqatools -> has special characters ['$', '$', '#']
python -> no special characters []

Learn the concept

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

More string programs

← All 106 String Programs

Remove duplicate characters

Python Practice › String Programs › Program 35

Remove duplicate characters

EasyString Programs · 35 of 106

Write a Python program to remove duplicate characters, keeping the first occurrence of each.

Input
"sqatools"
Output
sqatol

Approach

  1. dict.fromkeys keeps the first occurrence in order.

Solution

Python
text = "sqatools"print("".join(dict.fromkeys(text)))
Output
sqatol

Line by line

Line 2

Dictionary keys are unique and keep insertion order, so joining them gives the de-duplicated string.

Learn the concept

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

More string programs

← All 106 String Programs

Characters at odd indexes

Python Practice › String Programs › Program 34

Characters at odd indexes

EasyString Programs · 34 of 106

Write a Python program to print the characters at odd index positions.

Input
"abcdefg"
Output
bdf

Approach

  1. Slice from index 1 with a step of 2.

Solution

Python
print("abcdefg"[1::2])
Output
bdf

Learn the concept

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

More string programs

← All 106 String Programs

Count consonants

Python Practice › String Programs › Program 33

Count consonants

EasyString Programs · 33 of 106

Write a Python program to count the consonants in a string.

Input
"sqltools"
Output
6

Approach

  1. A consonant is a letter that is not a vowel.

Solution

Python
text = "sqltools"print(sum(1 for ch in text.lower() if ch.isalpha() and ch not in "aeiou"))
Output
6

Learn the concept

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

More string programs

← All 106 String Programs

Count vowels

Python Practice › String Programs › Program 32

Count vowels

EasyString Programs · 32 of 106

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

Input
"I am learning python"
Output
6

Approach

  1. Count characters that are in “aeiou”, ignoring case.

Solution

Python
text = "I am learning python"print(sum(1 for ch in text.lower() if ch in "aeiou"))
Output
6

Learn the concept

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

More string programs

← All 106 String Programs

Does a string contain a number?

Python Practice › String Programs › Program 31

Does a string contain a number?

EasyString Programs · 31 of 106

Write a Python program to check whether a string contains a digit.

Input
"python1", "python"
Output
python1 has a number
python has no number

Approach

  1. any(ch.isdigit() for ch in text).

Solution

Python
for text in ["python1", "python"]:    has = any(ch.isdigit() for ch in text)    print(text, "has a number" if has else "has no number")
Output
python1 has a number
python has no number

Line by line

Line 2

any() stops at the first True.

Learn the concept

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

More string programs

← All 106 String Programs

Characters at even indexes

Python Practice › String Programs › Program 30

Characters at even indexes

EasyString Programs · 30 of 106

Write a Python program to print the characters at even index positions (0, 2, 4, …).

Input
"sqatools"
Output
saol

Approach

  1. Slice with a step of 2.

Solution

Python
print("sqatools"[::2])
Output
saol

Line by line

Line 1

Starting at 0 with step 2 takes indexes 0, 2, 4 and 6.

Learn the concept

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

More string programs

← All 106 String Programs

Combine two strings

Python Practice › String Programs › Program 29

Combine two strings

EasyString Programs · 29 of 106

Write a Python program to combine two strings into one.

Input
"abc", "def"
Output
abcdef abcdef abcdef

Approach

  1. Use +.

Solution

Python
a, b = "abc", "def"print(a + b, f"{a}{b}", "".join([a, b]))
Output
abcdef abcdef abcdef

Learn the concept

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

More string programs

← All 106 String Programs

Frequency of each character

Python Practice › String Programs › Program 28

Frequency of each character

EasyString Programs · 28 of 106

Write a Python program to count how often each character appears in a string.

Input
"sqatools"
Output
{'s': 2, 'q': 1, 'a': 1, 't': 1, 'o': 2, 'l': 1}
True

Approach

  1. Build a dictionary of counts, or use Counter.

Solution

Python
text = "sqatools"freq = {}for ch in text:    freq[ch] = freq.get(ch, 0) + 1print(freq) from collections import Counterprint(Counter(text) == freq)
Output
{'s': 2, 'q': 1, 'a': 1, 't': 1, 'o': 2, 'l': 1}
True

Line by line

Line 4

get(ch, 0) handles the first time a character appears.

Learn the concept

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

More string programs

← All 106 String Programs

Length of a string

Python Practice › String Programs › Program 27

Length of a string

EasyString Programs · 27 of 106

Write a Python program to calculate the length of a string.

Input
"python"
Output
6

Approach

  1. Use len().

Solution

Python
print(len("python"))
Output
6

Learn the concept

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

More string programs

← All 106 String Programs