Python Remove Character from String

To remove characters from string in python; In this tutorial, you will learn how to remove first, last, and specific characters from string in python.

Whenever you work in any programming language. So many times you need to remove characters, special characters, and substrings from the string that you have. Such as remove string first character, remove string last character, remove any specific character in the string, remove multiple characters from string and remove specific index characters from string in python, etc.

How to remove a character from a string python

See the following python programs to how to remove first, last and specific character from string in python:

  • How to remove the last string character in a string.
  • How to remove the first character from the Python string.
  • Remove multiple characters from string in python.
  • How to remove a character from a specified index in a string.

Python has many built-in functions/methods for manipulation with strings, which you can checkout python string methods.

1: How to remove the last string character in a string

# Python code to remove last character from string

#take input string from user
str = input("Please Enter String : ")

# Remove last character
str = str[:-1]

# Print remaining string
print(str)

After executing the above program, the output will be:

Please Enter String :  my world....
Result :-  my world... 

2: How to remove the first string character in a string

# Python code to remove first character from string
# initializing test string
str='!my world'

# Remove first character
str = str[1:]

# Print remaining str
print(str)

After executing the above program, the output will be:

Please Enter String :  A programmer
Result :-   programmer

3: Remove multiple characters from string in python

# Python code to remove multiple characters from string

#take input string from user
str = input("Please Enter String : ")
removeChr = input("Please Enter Multiple Character, which you want to remove : ")

# Remove multiple characters
new_string = str
for character in removeChr:
  new_string = new_string.replace(character, "")

# Print remaining string
print('Result :- ', new_string)

After executing the above program, the output will be:

Please Enter String :  !(Hell@o)
Please Enter Multiple Character, which you want to remove :  !()@
Result :-  Hello

4: How to remove a character from a specified index in a string

# Python code to remove specific index character from string

#take input string from user
str = input("Please Enter String : ")
n = int(input("Please Enter Index of Character, which you want to remove : "))

# Remove nth character
x = str[:n]  # up to but not including n
y = str[n + 1:]  # n+1 till the end of string
str = x + y

# Print remaining string
print('Result :- ', str)

After executing the above program, the output will be:

Execution -1

Please Enter String :  python
Please Enter Index of Character, which you want to remove :  0
Result :-  ython

Execution -2

Please Enter String :  python
Please Enter Index of Character, which you want to remove :  1
Result :-  pthon

Recommended Python Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *