Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19k points)

There is a string, for example. EXAMPLE.

How can I remove the middle character, i.e., M from it? I don't need the code. I want to know:

  • Do strings in Python end in any special character?

  • Which is a better way - shifting everything right to left starting from the middle character OR creation of a new string and not copying the middle character?

1 Answer

0 votes
by (106k points)
edited by

Strings are immutable in Python, so you have to create a new string. 

newstr = oldstr.replace("M", "")

If you want to delete the middle character that you will do as follows:

midlen = len(oldstr)/2 

newstr = oldstr[:midlen] + oldstr[midlen+1:]

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
0 votes
1 answer
asked Sep 10, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...