I want to remove only one character from the string, what should I do?

Asked 2 years ago, Updated 2 years ago, 57 views

Str doesn't have del or pop. I just want to take out the characters in the middle of the string, for example,

To remove only the middle "M" from the string "EXAMPLE", which of the following methods would you recommend?

Also, when checking the end of the string, it was '\0' in c, but I wonder how it is done in Python

string python

2022-09-21 23:22

1 Answers

Python has two main options.

newstr = oldstr.replace("M", "")
midlen = len(oldstr)/2
newstr = oldstr[:midlen] + oldstr[midlen+1:]

And in C, the end of the string was always '\0', but in Python, there is no character that specifies the end, so we check it with len() or with an interpreter

mystr = "hello C world"

for i in range(len(mystr)):
    print(mystr[i])

#or Baro Eaterate
for i in mystr:
    print(i)


2022-09-21 23:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.