Float to 3 decimal places as a string

Python Practice › String Programs › Program 41

Float to 3 decimal places as a string

EasyString Programs · 41 of 106

Write a Python program to format a float to 3 decimal places and convert it to a string.

Input
2.14652
Output
2.147 str

Approach

  1. Use the format spec .3f.

Solution

Python
x = 2.14652text = f"{x:.3f}"print(text, type(text).__name__)
Output
2.147 str
Note

Formatting rounds, so 2.14652 becomes “2.147”, not “2.146” as the original sample showed. Use str(int(x * 1000) / 1000) if you need to cut off digits instead.

Learn the concept

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

More string programs

← All 106 String Programs

Split and join a string

Python Practice › String Programs › Program 40

Split and join a string

EasyString Programs · 40 of 106

Write a Python program to split a string into words and join them with “-“.

Input
"Hello world"
Output
['Hello', 'world']
Hello-world

Approach

  1. split() then "-".join().

Solution

Python
parts = "Hello world".split()print(parts)print("-".join(parts))
Output
['Hello', 'world']
Hello-world

Learn the concept

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

More string programs

← All 106 String Programs

Remove newlines from a string

Python Practice › String Programs › Program 39

Remove newlines from a string

EasyString Programs · 39 of 106

Write a Python program to remove newline characters from a string.

Input
"objectoriented\nprogramming\n"
Output
'objectoriented\nprogramming'
'objectorientedprogramming'

Approach

  1. strip() for the ends, replace("\n", "") for everywhere.

Solution

Python
text = "objectoriented\nprogramming\n"print(repr(text.rstrip("\n")))print(repr(text.replace("\n", "")))
Output
'objectoriented\nprogramming'
'objectorientedprogramming'

Line by line

Line 2

rstrip("\n") removes only trailing newlines.

Line 3

replace removes them everywhere.

Learn the concept

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

More string programs

← All 106 String Programs

Convert to uppercase

Python Practice › String Programs › Program 38

Convert to uppercase

EasyString Programs · 38 of 106

Write a Python program to convert all the characters in a string to uppercase.

Input
"I live in pune"
Output
I LIVE IN PUNE

Approach

  1. Use upper().

Solution

Python
print("I live in pune".upper())
Output
I LIVE IN PUNE

Learn the concept

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

More string programs

← All 106 String Programs

Swap the first and last letters of a string

Python Practice › String Programs › Program 37

Swap the first and last letters of a string

EasyString Programs · 37 of 106

Write a Python program to exchange the first and last letters of a string.

Input
"We are learning python"
Output
ne are learning pythoW

Approach

  1. last + middle + first.

Solution

Python
text = "We are learning python"print(text[-1] + text[1:-1] + text[0])
Output
ne are learning pythoW

Learn the concept

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

More string programs

← All 106 String Programs

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