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

Smallest and largest word

Python Practice › String Programs › Program 24

Smallest and largest word

EasyString Programs · 24 of 106

Write a Python program to find the smallest and largest word in a string.

Input
"Learning is a part of life and we strive"
Output
a Learning

Approach

  1. min and max with key=len.

Solution

Python
words = "Learning is a part of life and we strive".split()print(min(words, key=len), max(words, key=len))
Output
a Learning

Learn the concept

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

More string programs

← All 106 String Programs

Common words in two strings

Python Practice › String Programs › Program 23

Common words in two strings

EasyString Programs · 23 of 106

Write a Python program to find the words that appear in both strings, ignoring punctuation.

Input
"Very Good Morning, How are You" and "You are a Good student, keep it up"
Output
['Good', 'You', 'are']

Approach

  1. Strip punctuation, split into sets, and intersect.

Solution

Python
s1 = "Very Good Morning, How are You"s2 = "You are a Good student, keep it up"w1 = {w.strip(",.") for w in s1.split()}w2 = {w.strip(",.") for w in s2.split()}print(sorted(w1 & w2))
Output
['Good', 'You', 'are']

Line by line

Lines 3-4

strip(",.") removes punctuation stuck to a word, such as “Morning,”.

Line 5

& keeps the words present in both sets.

Learn the concept

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

More string programs

← All 106 String Programs

Longest all-capital word

Python Practice › String Programs › Program 22

Longest all-capital word

EasyString Programs · 22 of 106

Write a Python program to find the longest word written in capital letters.

Input
"Learning PYTHON programming is FUN"
Output
PYTHON

Approach

  1. Filter words with isupper(), then take the longest.

Solution

Python
text = "Learning PYTHON programming is FUN"caps = [w for w in text.split() if w.isupper()]print(max(caps, key=len) if caps else None)
Output
PYTHON

Line by line

Line 3

The conditional expression avoids an error when no word is in capitals.

Learn the concept

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

More string programs

← All 106 String Programs