Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 739
pythoncoder Avatar asked Apr 05 '19 03:04

pythoncoder


People also ask

How to replace the first character in a string in Python?

For just replacing the first character in the string, you need to pass maxreplace as 1. For example: >>> s = "Python is programming language" >>> s.replace ('n', 'o', 1) 'Pythoo is programming language' # ^ Here first "n" is replaced with "o" Return a copy of string s with all occurrences of substring old replaced by new.

How to replace first n occurrences of a character in a string?

Replace first n occurrence of a character in a string To replace the first n occurrences of characters of a string with another character, we can specify the optional input argument in the replace() method.

What is the use of replace () method in Python?

Python possesses many in-built functions to make programming easy and replace () method is one of them. replace () method helps to replace the occurrence of the given old character with the new character or substring.

How do I get the second character in a string?

The text [1:] returns the string in text from position 1 to the end, positions count from 0 so '1' is the second character. edit: You can use the same string slicing technique for any part of the string Or if the letter only appears once you can use the search and replace technique suggested below


3 Answers

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.

like image 86
lmiguelvargasf Avatar answered Nov 14 '22 23:11

lmiguelvargasf


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

like image 29
nathancy Avatar answered Nov 14 '22 23:11

nathancy


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)
like image 28
U12-Forward Avatar answered Nov 14 '22 21:11

U12-Forward