w3resource

Python Exercises: Capitalize the first letter and lowercases the rest

Python String: Exercise-104 with Solution

Write a Python program that capitalizes the first letter and lowercases the remaining letters of a given string.

Sample Data:
(“Red Green WHITE) -> “Red Green White”
(“w3resource”) -> “W3resource”
(“dow jones industrial average”) -> “Dow Jones Industrial Average”

Sample Solution-1:

Python Code:

def test(strs):
    return ' '.join(word.capitalize() for word in strs.split())    
text ="Red Green WHITE"
print("Original string:", text)
print("Capitalize the first letter and lowercases the rest:")
print(test(text))
text = "w3resource"
print("\nOriginal string:", text)
print("Capitalize the first letter and lowercases the rest:")
print(test(text))
text = "dow jones industrial average"
print("\nOriginal string:", text)
print("Capitalize the first letter and lowercases the rest:")
print(test(text)) 

Sample Output:

Original string: Red Green WHITE
Capitalize the first letter and lowercases the rest:
Red Green White

Original string: w3resource
Capitalize the first letter and lowercases the rest:
W3resource

Original string: dow jones industrial average
Capitalize the first letter and lowercases the rest:
Dow Jones Industrial Average

Flowchart:

Flowchart: Capitalize the first letter and lowercases the rest.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Sample Solution-2:

Python Code:

def test(strs):
    return ' '.join(i[0].upper() + i[1:].lower() for i in strs.split())   
text ="Red Green WHITE"
print("Original string:", text)
print("Capitalize the first letter and lowercases the rest:")
print(test(text))
text = "w3resource"
print("\nOriginal string:", text)
print("Capitalize the first letter and lowercases the rest:")
print(test(text))
text = "dow jones industrial average"
print("\nOriginal string:", text)
print("Capitalize the first letter and lowercases the rest:")
print(test(text))

Sample Output:

Original string: Red Green WHITE
Capitalize the first letter and lowercases the rest:
Red Green White

Original string: w3resource
Capitalize the first letter and lowercases the rest:
W3resource

Original string: dow jones industrial average
Capitalize the first letter and lowercases the rest:
Dow Jones Industrial Average

Flowchart:

Flowchart: Capitalize the first letter and lowercases the rest.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous Python Exercise: Replace a word with hash characters in a string.
Next Python Exercise: Extract the name from an Email address.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Inserting if statements using conditional list comprehensions:

x = [1, 2, 3, 4, 5, 6]
result = []
for idx in range(len(x)):
    if x[idx] % 2 == 0:
        result.append(x[idx] * 2)
    else:
        result.append(x[idx])
result

Output:

[1, 4, 3, 8, 5, 12]
[(element * 2 if element % 2 == 0 else element) for element in x]

Output:

[1, 4, 3, 8, 5, 12]
[element * 2 for element in x if element % 2 == 0]

Output:

[4, 8, 12]