+ 6

How can I delete a character from a string?

21st Oct 2020, 3:21 PM
Nafis Sadiq Bhuyan
Nafis Sadiq Bhuyan - avatar
9 Answers
+ 9
U can use PYTHONs replace method like this... //Python3 STRING = "hello world" STRING.replace("h", "") print(STRING) //Output ello world
21st Oct 2020, 3:37 PM
Steve Sajeev
Steve Sajeev - avatar
+ 5
print("text here".replace("here", "")) Outputs: "text "
21st Oct 2020, 3:36 PM
かんでん
かんでん - avatar
+ 4
Original string won't change ,so you need to create a new variable to store the new string returned by replace method STRING=STRING.replace("h","")
21st Oct 2020, 3:48 PM
Abhay
Abhay - avatar
+ 4
string are immutable, which means you cant change it’s content directly. you can work with the string as a list to delete the charecter using a for loop and remove() and then join them together using ‘’.joiin().
21st Oct 2020, 3:56 PM
Abdulaziz_albusaidi
Abdulaziz_albusaidi - avatar
+ 4
Steve Sajeev I think this method wont work properly with duplicates.
21st Oct 2020, 4:04 PM
Abdulaziz_albusaidi
Abdulaziz_albusaidi - avatar
+ 4
Thanks a ton
21st Oct 2020, 4:14 PM
Nafis Sadiq Bhuyan
Nafis Sadiq Bhuyan - avatar
+ 4
Abdulaziz_albusaidi Yes are right, as long as I am correct, it would become little advanced. Thats why I didnt Include I dont know anyother methods
21st Oct 2020, 5:42 PM
Steve Sajeev
Steve Sajeev - avatar
+ 1
s = "corre1ct" s = s[:5] + s[6:] print(s)
21st Oct 2020, 7:49 PM
portpass
+ 1
String is immutable
23rd Oct 2020, 5:41 AM
Antony Christu Raj J
Antony Christu Raj J - avatar