w3resource

Python: Lowercase first n characters in a string


Lowercase first n characters of string.

Write a Python program to lowercase the first n characters in a string.

Python String Exercises: Lowercase first n characters in a string

Sample Solution:

Python Code:

# Define a string 'str1'.
str1 = 'W3RESOURCE.COM'

# Convert the first four characters of the string to lowercase and concatenate them with the remaining characters.
# Print the modified string.
print(str1[:4].lower() + str1[4:])

Sample Output:

w3reSOURCE.COM                    

Flowchart:

Flowchart: Lowercase first n characters in a string

For more Practice: Solve these Related Problems:

  • Write a Python program to convert the first n characters of an input string to lowercase using slicing.
  • Write a Python program to implement a function that lowercases the first n characters and leaves the rest unchanged.
  • Write a Python program to use a loop to process and convert only the first n characters of a string to lowercase.
  • Write a Python program to conditionally apply lower() to the first n characters of a string if n is less than the string's length.

Python Code Editor:

Previous: Write a Python program to convert a given string into a list of words.
Next: Write a Python program to swap comma and dot in a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.