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

Reverse each word

Python Practice › String Programs › Program 26

Reverse each word

EasyString Programs · 26 of 106

Write a Python program to reverse each word of a string, keeping the word order.

Input
"sqatools python"
Output
slootaqs nohtyp

Approach

  1. Reverse every word and join them back.

Solution

Python
text = "sqatools python"print(" ".join(w[::-1] for w in text.split()))
Output
slootaqs nohtyp

Learn the concept

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

More string programs

← All 106 String Programs

Check a palindrome string

Python Practice › String Programs › Program 25

Check a palindrome string

EasyString Programs · 25 of 106

Check whether a given string is a palindrome.

Input
"sqatoolssqatools", "racecar"
Output
sqatoolssqatools is not a palindrome
racecar is a palindrome

Approach

  1. Compare the string with its reverse.

Solution

Python
for s in ["sqatoolssqatools", "racecar"]:    print(s, "is a palindrome" if s == s[::-1] else "is not a palindrome")
Output
sqatoolssqatools is not a palindrome
racecar is a palindrome

Learn the concept

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

More string programs

← All 106 String Programs