Replace First Character of a String in Python

This article will discuss different ways to replace only the first character of a string in Python.

Table Of Contents

Suppose we have a string,

"Sample"

After replacing the first character of this string with “X”, the final string should be,

"Xample"

There are different ways to replace only the first character of a string in Python. Let’s discuss them one by one.

Using Indexing

We can select the sub-strings from a string based on Python’s index range and subscript operator. For example, str[start:end] will select the substring from index position start to end.

Using this concept, we can select the string from index position one until the end of the string, i.e. str[1:]. It will give us a substring containing all the characters of the original string except the first character. Then we can add this string after a new character. It will give us the effect that we have replaced the first character of a string.

For Example:

strValue = 'Sample'

# Replace first character of string with 'X'
strValue = 'X' + strValue[1:]

print(strValue)

Output:

Xample

It replaced the string’s first character with the character ‘X’.

Using replace()

In Python, the string class provides a function replace(substring, replacement, count) to change the contents of string. It accepts two arguments,

  • substring
  • replacement
  • count

It replaces the first count occurrences of substring in string with the replacement substring. We can use this to replace only the first occurrence of first character in string.

For Example:

strValue = 'Sample'

# Replace first character of string with 'X'
strValue = strValue.replace(strValue[0], 'X', 1)

print(strValue)

Output:

Xample

It replaced the string’s first character with the character ‘X’.

Using List

As Strings are immutable in Python, therefore we can not directly change the content of a string using the subscript operator i.e.

strValue = 'Sample'
strValue[0] = 'X'

It will give errors like,

TypeError: 'str' object does not support item assignment.

But we can convert the string to a list and then change the value of the first character. Then we can cast it back to string from the list.

For Example:

strValue = 'Sample'

# Convert string to list
listOfChars = list(strValue)

# Replace first character in list with 'X'
listOfChars[0] = 'X'

# Convert the list to string
strValue = ''.join(listOfChars)

print(strValue)

Output:

Xample

It replaced the string’s first character with the character ‘X’.

Using Regex

The regex module in Python, provides a function regex.sub(pattern, replacement_str, original_str) to replace the contents of a string based on a matching regex pattern.

To replace only the first character in a string, we will pass the regex pattern “^.” and replacement character in sub() function. This regex pattern will match only the first character in the string and will be replaced by the given character.

For Example:

import re

strValue = 'Sample'

# Replace first character of string with 'X'
strValue = re.sub(r'^.', 'X', strValue)

print(strValue)

Output:

Xample

It replaced the string’s first character with the character ‘X’.

Summary:

We learned different ways to replace the first character of a string in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top