{"id":4732,"date":"2021-04-29T18:19:46","date_gmt":"2021-04-29T12:49:46","guid":{"rendered":"https:\/\/python-programs.com\/?p=4732"},"modified":"2021-11-22T18:53:41","modified_gmt":"2021-11-22T13:23:41","slug":"python-how-to-remove-characters-from-a-string-by-index","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/","title":{"rendered":"Python : How to remove characters from a string by Index ?"},"content":{"rendered":"
Here we are going to discuss how to remove characters from a string in a given range of indices or at specific index position.<\/p>\n
So we will discuss different different methods for this.<\/p>\n
In this method, we have to first run the loop and append the characters .After that\u00a0 build a new string from the existing one .<\/p>\n
test_str = \"WelcomeBtechGeeks\"\r\n \r\n# Printing original string \r\nprint (\"The original string is : \" + test_str)\r\n \r\n# Removing char at pos 3\r\n# using loop\r\nnew_str = \"\"\r\n \r\nfor i in range(len(test_str)):\r\n if i != 2:\r\n new_str = new_str + test_str[i]\r\n \r\n# Printing string after removal \r\nprint (\"The string after removal of i'th character : \" + new_str)<\/pre>\nOutput:<\/strong><\/p>\n
RESTART: C:\/Users\/HP\/Desktop\/article3.py\r\nThe original string is : WelcomeBtechGeeks\r\nThe string after removal of i'th character : WecomeBtechGeeks\r\n<\/pre>\nSo in above eoutput you have seen that we have remove character of position three that is ‘l’.<\/p>\n
This method is very slow if we compare with other methods.<\/p>\n
Using str.replace()<\/h3>\n
str.replace() can replace the particular index with empty char, and hence solve the issue.<\/p>\n
test_str = \"WelcomeBtechGeeks\"\r\n \r\n# Printing original string \r\nprint (\"The original string is : \" + test_str)\r\n \r\n# Removing char at pos 3\r\n# using replace\r\nnew_str = test_str.replace('e', '1')\r\n \r\n# Printing string after removal \r\n# removes all occurrences of 'e'\r\nprint (\"The string after removal of i'th character( doesn't work) : \" + new_str)\r\n \r\n# Removing 1st occurrence of e, i.e 2nd pos.\r\n# if we wish to remove it.\r\nnew_str = test_str.replace('e', '', 1)\r\n \r\n# Printing string after removal \r\n# removes first occurrences of e\r\nprint (\"The string after removal of i'th character(works) : \" + new_str)\r\n\r\n<\/pre>\nOutput:<\/strong><\/p>\n
RESTART: C:\/Users\/HP\/Desktop\/article3.py\r\nThe original string is : WelcomeBtechGeeks\r\nThe string after removal of i'th character( doesn't work) : W1lcom1Bt1chG11ks\r\nThe string after removal of i'th character(works) : WlcomeBtechGeeks\r\n<\/pre>\nSo in above output you can see that first we have replace all ‘e’ present in original word.After that we replace only first occurrence of e.This method is also not very useful but sometime we are using this.<\/p>\n
Using slice + concatenation<\/h3>\n
In this method we will use string slicing.Then using string concatenation of both, i\u2019th character can appear to be deleted from the string.<\/p>\n
test_str = \"WelcomeBtechGeeks\"\r\n \r\n# Printing original string \r\nprint (\"The original string is : \" + test_str)\r\n \r\n#Removing char at pos 3\r\n# using slice + concatenation\r\nnew_str = test_str[:2] + test_str[3:]\r\n \r\n# Printing string after removal \r\n# removes ele. at 3rd index\r\nprint (\"The string after removal of i'th character : \" + new_str)\r\n\r\n<\/pre>\nOutput:<\/strong><\/p>\n
RESTART: C:\/Users\/HP\/Desktop\/article3.py\r\nThe original string is : WelcomeBtechGeeks\r\nThe string after removal of i'th character : WecomeBtechGeeks\r\n\r\n<\/pre>\nUsing\u00a0str.join() and list comprehension<\/h3>\n
In this method each string is converted in list then each of them is joined to make a string.<\/p>\n
test_str = \"WelcomeBtechGeeks\"\r\n \r\n# Printing original string \r\nprint (\"The original string is : \" + test_str)\r\n \r\n# Removing char at pos 3\r\n# using join() + list comprehension\r\nnew_str = ''.join([test_str[i] for i in range(len(test_str)) if i != 2])\r\n \r\n# Printing string after removal \r\n# removes ele. at 3rd index\r\nprint (\"The string after removal of i'th character : \" + new_str)\r\n\r\n<\/pre>\nOutput:<\/strong><\/p>\n
RESTART: C:\/Users\/HP\/Desktop\/article3.py\r\nThe original string is : WelcomeBtechGeeks\r\nThe string after removal of i'th character : WecomeBtechGeeks\r\n\r\n<\/pre>\nConclusion:<\/h3>\n
So in this article we have seen different method to remove characters from a string in a given range of indices or at specific index position.Enjoy learning guys.<\/p>\n","protected":false},"excerpt":{"rendered":"
Ways to remove i\u2019th character from string in Python Here we are going to discuss how to remove characters from a string in a given range of indices or at specific index position. So we will discuss different different methods for this. Naive Method In this method, we have to first run the loop and …<\/p>\n