Uppercase the second half

Python Practice › String Programs › Program 89

Uppercase the second half

EasyString Programs · 89 of 106

Write a Python program to convert the second half of a string to uppercase.

Input
"banana"
Output
banANA

Approach

  1. Split at the midpoint.

Solution

Python
text = "banana"mid = len(text) // 2print(text[:mid] + text[mid:].upper())
Output
banANA

Learn the concept

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

More string programs

← All 106 String Programs

Leap years in a range given as text

Python Practice › String Programs › Program 87

Leap years in a range given as text

EasyString Programs · 87 of 106

Write a Python program to count the leap years in a range given as a string like “1981-2001”.

Input
"1981-2001"
Output
Total leap years: 5 [1984, 1988, 1992, 1996, 2000]

Approach

  1. Split the text, convert both years, count with calendar.isleap.

Solution

Python
import calendar text = "1981-2001"start, end = map(int, text.split("-"))leaps = [y for y in range(start, end + 1) if calendar.isleap(y)]print("Total leap years:", len(leaps), leaps)
Output
Total leap years: 5 [1984, 1988, 1992, 1996, 2000]

Line by line

Line 4

map(int, ...) converts both parts.

Learn the concept

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

More string programs

← All 106 String Programs

Name from an email address

Python Practice › String Programs › Program 86

Name from an email address

EasyString Programs · 86 of 106

Write a Python program to extract the name part of an email address (without trailing digits).

Input
"student1@gmail.com"
Output
student

Approach

  1. Take the part before “@”, then strip digits from the end.

Solution

Python
email = "student1@gmail.com"name = email.split("@")[0].rstrip("0123456789")print(name)
Output
student

Learn the concept

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

More string programs

← All 106 String Programs

Add two numeric strings

Python Practice › String Programs › Program 85

Add two numeric strings

EasyString Programs · 85 of 106

Write a Python program to add two strings as numbers and return a string.

Input
"3", "7"
Output
'10' | without conversion: 37

Approach

  1. Convert to int, add, convert back.

Solution

Python
a, b = "3", "7"print(repr(str(int(a) + int(b))), "| without conversion:", a + b)
Output
'10' | without conversion: 37

Learn the concept

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

More string programs

← All 106 String Programs

Split a multi-line string into lines

Python Practice › String Programs › Program 84

Split a multi-line string into lines

EasyString Programs · 84 of 106

Write a Python program to split a multi-line string into a list of lines.

Input
a three-line string
Output
['This string Contains', 'Multiple', 'Lines']

Approach

  1. splitlines().

Solution

Python
text = """This string ContainsMultipleLines"""print(text.splitlines())
Output
['This string Contains', 'Multiple', 'Lines']

Learn the concept

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

More string programs

← All 106 String Programs

Extract numbers as integers

Python Practice › String Programs › Program 83

Extract numbers as integers

EasyString Programs · 83 of 106

Write a Python program to extract the numbers from a string.

Input
"python 456 self learning 89"
Output
[456, 89]

Approach

  1. Keep words that are digits and convert them.

Solution

Python
text = "python 456 self learning 89"print([int(w) for w in text.split() if w.isdigit()])
Output
[456, 89]

Learn the concept

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

More string programs

← All 106 String Programs

Similarity of two strings

Python Practice › String Programs › Program 82

Similarity of two strings

MediumString Programs · 82 of 106

Write a Python program to measure how similar two strings are.

Input
"Learning is fun in Sqatools", "Sqatools Online Learning Platform"
Output
Similarity: 0.4
Word overlap (Jaccard): 0.29

Approach

  1. difflib.SequenceMatcher returns a ratio from 0 (different) to 1 (identical).

Solution

Python
from difflib import SequenceMatcher s1 = "Learning is fun in Sqatools"s2 = "Sqatools Online Learning Platform"ratio = SequenceMatcher(None, s1, s2).ratio()print("Similarity:", round(ratio, 2)) w1, w2 = set(s1.lower().split()), set(s2.lower().split())print("Word overlap (Jaccard):", round(len(w1 & w2) / len(w1 | w2), 2))
Output
Similarity: 0.4
Word overlap (Jaccard): 0.29

Line by line

Line 5

The ratio compares matching blocks of characters.

Lines 8-9

A second measure compares shared words over all distinct words.

Note

Different similarity measures give different numbers, so state which one you use.

Learn the concept

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

More string programs

← All 106 String Programs

Remove symbols and capitalise

Python Practice › String Programs › Program 81

Remove symbols and capitalise

EasyString Programs · 81 of 106

Write a Python program to remove unwanted characters from a string and capitalise it.

Input
"sqa****too^^{ls"
Output
Sqatools

Approach

  1. Keep letters only, then capitalize().

Solution

Python
text = "sqa****too^^{ls"print("".join(ch for ch in text if ch.isalpha()).capitalize())
Output
Sqatools

Learn the concept

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

More string programs

← All 106 String Programs

Number of non-empty substrings

Python Practice › String Programs › Program 80

Number of non-empty substrings

EasyString Programs · 80 of 106

Write a Python program to count the non-empty substrings of a string.

Input
"sqatools12"
Output
55
55

Approach

  1. A string of length n has n(n + 1)/2 non-empty substrings.

Solution

Python
text = "sqatools12"n = len(text)print(n * (n + 1) // 2)print(sum(1 for i in range(n) for j in range(i + 1, n + 1)))
Output
55
55

Line by line

Line 3

The formula counts every pair of start and end positions.

Line 4

The nested loops count them one by one and confirm the formula.

Learn the concept

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

More string programs

← All 106 String Programs

Count uppercase, lowercase, digits and specials

Python Practice › String Programs › Program 79

Count uppercase, lowercase, digits and specials

EasyString Programs · 79 of 106

Write a Python program to count the uppercase, lowercase, special and numeric characters in a string.

Input
"@SqaTools.lin"
Output
Special characters: 2
Uppercase characters: 2
Lowercase characters: 9
Numeric values: 0

Approach

  1. Test each character and increase the matching counter.

Solution

Python
text = "@SqaTools.lin"upper = lower = digits = special = 0for ch in text:    if ch.isupper():        upper += 1    elif ch.islower():        lower += 1    elif ch.isdigit():        digits += 1    else:        special += 1print("Special characters:", special)print("Uppercase characters:", upper)print("Lowercase characters:", lower)print("Numeric values:", digits)
Output
Special characters: 2
Uppercase characters: 2
Lowercase characters: 9
Numeric values: 0
Note

“@SqaTools.lin” has 2 special characters (@ and .) and 9 lowercase letters; the original sample showed 1 and 8.

Learn the concept

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

More string programs

← All 106 String Programs