Skip to main content

How to Replace First N Characters in Python Strings

Replacing the first character, or the first several characters, of a string is a common task in Python.

This guide explores various methods to achieve this, including string slicing, str.replace(), regular expressions (re.sub()), and list conversion, highlighting the best practices for each technique.

String slicing combined with concatenation is the most direct and Pythonic way to replace the first N characters:

my_str = 'tutorialreference.com'

# Replace the first character
replacement_char = '_'
new_str = replacement_char + my_str[1:]
print(new_str) # Output: _utorialreference.com

# Replace the first 5 characters
n = 5
replacement_prefix = 'new_'
new_str_n = replacement_prefix + my_str[n:]
print(new_str_n) # Output: new_ialreference.com
  • my_str[n:] creates a slice starting from index n (the character after the ones you want to replace) to the end of the string.
  • The + operator concatenates your replacement_prefix with the rest of the original string.
  • If the replacement value isn't a string, convert it first using str().

Replacing First Occurrence with str.replace()

The str.replace() method can replace the first occurrence of a specific substring by setting the count argument to 1:

my_str = 'tutorialreference.com'

# Replace the first 't'
new_str = my_str.replace('t', '_', 1)
print(new_str) # Output: _utorialreference.com

# Replace the first 'tut'
new_str_n = my_str.replace('tut', '___', 1)
print(new_str_n) # Output: ___orialreference.com
  • replace(old, new, 1) replaces only the first instance of old with new.

To replace the actual first N characters regardless of their value, combine slicing with replace():

my_str = 'tutorialreference.com'
n = 3
new_str = my_str.replace(my_str[:n], '___', 1) # Replace the first 3 characters
print(new_str) # Output: ___orialreference.com

Replacing First Occurrence with re.sub()

Regular expressions offer another way to replace the first occurrence, also using a count argument:

import re

my_str = 'tutorialreference.com'

# Replace the first 't'
new_str = re.sub('t', '_', my_str, count=1)
print(new_str) # Output: _utorialreference.com

# Replace the first 'tut'
new_str_n = re.sub('tut', '___', my_str, count=1)
print(new_str_n) # Output: ___orialreference.com
  • re.sub(pattern, replacement, string, count=1) replaces the first match of the pattern.
  • This is more powerful if you need to match a pattern at the beginning, not just a literal string. For simple prefix replacement, str.replace() or slicing are often clearer.

You can convert the string to a list, modify the list elements, and join back. This is generally less efficient and less readable than slicing for this specific task.

my_str = 'tutorialreference.com'
list_of_chars = list(my_str) # Convert to list

# Replace first character
list_of_chars[0] = '_'
new_str = ''.join(list_of_chars)
print(new_str) # Output: _utorialreference.com

# Replace first N characters
my_str = 'tutorialreference.com'
list_of_chars = list(my_str)
n = 3
list_of_chars[:n] = '___' # Replace slice with new characters
new_str_n = ''.join(list_of_chars)
print(new_str_n) # Output: ___orialreference.com
  • Strings are immutable, so direct modification isn't possible. This method creates intermediate lists.

Conclusion

This guide demonstrated several ways to replace the first or first N characters of a Python string.

String slicing combined with concatenation (replacement + my_str[n:]) is the most direct, readable, and generally recommended approach.

  • str.replace() and re.sub() with a count of 1 are suitable for replacing the first occurrence of a specific substring or pattern.
  • Converting to a list is less efficient and typically unnecessary for this task.