Replacing a char at provided index in a string [duplicate] - python

This question already has answers here:
Replacing instances of a character in a string
(17 answers)
Changing one character in a string
(15 answers)
Closed 4 years ago.
So I have this string: "My Wurds" and I'd like to replace the u at index 4 with o. what is the proper Python way to do that?

You could build a new string using slices:
s = "My Wurds"
t = s[:4] + "o" + s[5:]

If you know the index of u where you need to replace with o, a pretty straight approach would be:
s[:4] + s[4:].replace('u', 'o', 1)
replace('u', 'o', 1) will ensure replace of only first occurance.

Related

How to remove all characters within a string after a non-alphanumerical character [duplicate]

This question already has answers here:
How to remove all characters after a specific character in python?
(10 answers)
Closed 1 year ago.
I want to remove all the character after a non-alphanumerical character ('_') within a string.
For example:
Petr_;Y -> Petr
ČEZ_^(České_energetické_závody) -> ČEZ
I tried:
''.join(c for c in mystring if c.isalnum())
But this way I'm stripping off only alphanumerical characters itself.
Help would be appreciated.
You may want to use the .split() method on strings.
new_string = your_string.split('_',1)[0]
This way you keep only what's before the fisrt '_'.
Searching the index of first occurrence of "_" will do:
s1 = "Petr_;Y"
s2 = "ČEZ_^(České_energetické_závody)"
s11 = s1[:s1.index("_")]
s22 = s2[:s2.index("_")]

How to replace several overlapped characters with one in Python [duplicate]

This question already has answers here:
How do I coalesce a sequence of identical characters into just one?
(10 answers)
Closed 2 years ago.
I have a string, something like that (I don't know in advance how much similar characters in a sequence):
s = '&&&&&word&&&word2&&&'
and would like to obtain as a result this string:
'&word&word2&'
Workaround is something like this (not effective I guess for large texts):
while True:
if not '&&' in s:
break
s = s.replace('&&','&')
You can use a regex to replace any occurence of one or more '&' (&+) by '&':
import re
s = '&&&&&word&&&word2&&&'
res = re.sub(r'&+', '&', s)
print(res)
# &word&word2&

How to remove a character from a string until certain index? [duplicate]

This question already has answers here:
Remove characters from beginning and end or only end of line
(5 answers)
Closed 4 years ago.
So, I have the following string "........my.python.string" and I want to remove all the "." until it gets to the first alphanumeric character, is there a way to achieve this other than converting the string to a list and work it from there?
You can use re.sub:
import re
s = "........my.python.string"
new_s = re.sub('^\.+', '', s)
print(new_s)
Output:
my.python.string

How can I add a space after a specific character in a string? [duplicate]

This question already has answers here:
Replacing instances of a character in a string
(17 answers)
Closed 6 years ago.
I need to add a space between X in a string. The program takes measurements in one field and I need to be able to separate integers from the "x" before doing the calculation.
For example: "12x24" should read "12 x 24"
Replace 'x' with '<space>x<space>' using str.replace() function as:
>>> my_str = '12x24'
>>> my_str.replace('x', ' x ')
'12 x 24'
Use the replace method to substitute ' x ' for 'x':
string.replace('x', ' x ')

Finding all the indexes of a substring given a string containing it in Python [duplicate]

This question already has answers here:
How to get the position of a character in Python?
(11 answers)
Closed 7 years ago.
For example:
my_string = "hi how are you?/nIs everything ok?/nAre you happy?"
I need to make a list containing all the indexes of the newline - (/n).
How can i do it ?
You can use enumerate in a list comprehension to create a list of indices.
>>> [index for index, value in enumerate(my_string) if value == '\n']
[15, 33]
By the way, a new line character is '\n' not '/n' (note the slash)
import re
my_string = "hi how are you?/nIs everything ok?/nAre you happy?"
list = [m.start() for m in re.finditer('/n', my_string)]

Categories

Resources