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

First repeated word

Python Practice › String Programs › Program 68

First repeated word

EasyString Programs · 68 of 106

Write a Python program to find the first word that repeats.

Input
"ab bc ca ab bd"
Output
ab

Approach

  1. Remember words seen; return the first one seen again.

Solution

Python
def first_repeated(text):    seen = set()    for w in text.split():        if w in seen:            return w        seen.add(w)    return None print(first_repeated("ab bc ca ab bd"))
Output
ab

Learn the concept

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

More string programs

← All 106 String Programs

Split on the last delimiter

Python Practice › String Programs › Program 67

Split on the last delimiter

EasyString Programs · 67 of 106

Write a Python program to split a string on the last occurrence of a delimiter.

Input
"l,e,a,r,n,I,n,g,p,y,t,h,o,n"
Output
['l,e,a,r,n,I,n,g,p,y,t,h,o', 'n']

Approach

  1. rsplit(",", 1) splits once, from the right.

Solution

Python
text = "l,e,a,r,n,I,n,g,p,y,t,h,o,n"print(text.rsplit(",", 1))
Output
['l,e,a,r,n,I,n,g,p,y,t,h,o', 'n']

Learn the concept

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

More string programs

← All 106 String Programs

Count and show vowels

Python Practice › String Programs › Program 66

Count and show vowels

EasyString Programs · 66 of 106

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

Input
"welcome to Sqatools"
Output
['e', 'o', 'e', 'o', 'a', 'o', 'o'] 7

Approach

  1. Collect the vowels in a list.

Solution

Python
text = "welcome to Sqatools"vowels = [ch for ch in text if ch.lower() in "aeiou"]print(vowels, len(vowels))
Output
['e', 'o', 'e', 'o', 'a', 'o', 'o'] 7

Learn the concept

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

More string programs

← All 106 String Programs

Swap commas and dots

Python Practice › String Programs › Program 65

Swap commas and dots

EasyString Programs · 65 of 106

Write a Python program to swap commas and dots.

Input
"sqa,tools.python"
Output
sqa.tools,python

Approach

  1. translate with a two-way mapping swaps them in one pass.

Solution

Python
text = "sqa,tools.python"print(text.translate(str.maketrans(",.", ".,")))
Output
sqa.tools,python

Line by line

Line 2

maketrans(",.", ".,") maps , to . and . to , at the same time; two replace calls would undo each other.

Learn the concept

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

More string programs

← All 106 String Programs

String to a list of words

Python Practice › String Programs › Program 64

String to a list of words

EasyString Programs · 64 of 106

Write a Python program to convert a string into a list of words.

Input
"learning python is fun"
Output
['learning', 'python', 'is', 'fun']

Approach

  1. split().

Solution

Python
print("learning python is fun".split())
Output
['learning', 'python', 'is', 'fun']

Learn the concept

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

More string programs

← All 106 String Programs