17

How to replace the first character alone in a string using python?

string = "11234"
translation_table = str.maketrans({'1': 'I'})
output= (string.translate(translation_table))
print(output)

Expected Output:

I1234

Actual Ouptut:

11234
4
  • 6
    can't you just do something like 'I' + string[1:] ? Commented Apr 5, 2019 at 3:16
  • 1
    Note: Your "Got Output" should be II234, not 11234, unless you're saying translate did nothing. I copied and pasted the code and got II234 as expected. Commented Apr 5, 2019 at 3:27
  • I find this question confusing, and all the answers below incorrect if I interpret "first character" as the first character of the string, not the first matching character. The solutions provided would replace "21234" with "2I234", which is not the first character of the string. The OP's intention is ambiguous because they selected one of the incorrect answers as the right one. Can you clarify whether you intended "first character" or "first occurrence"? Commented Jun 25, 2023 at 12:57
  • @lmiguelvargasf your solution would work but only with a condition, otherwise it would replace any character with I, and the OP specified a translation table. Commented Jun 25, 2023 at 13:00

5 Answers 5

17

I am not sure what you want to achive, but it seems you just want to replace a '1' for an 'I' just once, so try this:

string = "11234"
string.replace('1', 'I', 1)

str.replace takes 3 parameters old, new, and count (which is optional). count indicates the number of times you want to replace the old substring with the new substring.

Sign up to request clarification or add additional context in comments.

Comments

10

In Python, strings are immutable meaning you cannot assign to indices or modify a character at a specific index. Use str.replace() instead. Here's the function header

str.replace(old, new[, count])

This built in function returns a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

If you don't want to use str.replace(), you can manually do it by taking advantage of splicing

def manual_replace(s, char, index):
    return s[:index] + char + s[index +1:]

string = '11234'
print(manual_replace(string, 'I', 0))

Output

I1234

1 Comment

this answer basically is very similar to mine just that this provides a references to that method....
3

You can use re (regex), and use the sub function there, first parameter is the thing you want to replace, and second is the thing that you want to replace with, third is the string, fourth is the count, so i say 1 because you only want the first one:

>>> import re
>>> string = "11234"
>>> re.sub('1', 'I', string, 1)
'I1234'
>>> 

It's virtually just:

re.sub('1', 'I', string, 1)

Comments

3

by reading your question, I understand you want to replace a certain character of your sting on a certain index, in your case replace character on index [0] with 'I',

my answer:

well as we know string's are immutable so we cannot just modify and replace a character directly, but their is a way to go around this, steps:

  1. convert your string into a list
  2. change the character on specified index
  3. convert back into a sting

code:

string = "11234"
string_asList = list(string)       #converts sting into list

string_asList[0] = "I"             #replace element at [O] index
string = ''.join(string_asList)    #converts list back to string

print(string)

Comments

0

If I am interpreting the OP's "first character" correctly, this should replace the first and only the first character of the string if matching:

string = "11234"
translation_table = str.maketrans({'1': 'I'})
output = string[0].translate(translation_table) + string[1:]
print(output)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.