Given a very large string. I would like to process parts of the string in a loop like this:
large_string = "foobar..."
while large_string:
process(large_string.pop(200))
What is a nice and efficient way of doing this?
The pop() method is generic. It only expects the this value to have a length property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.
Python Remove Character from String using replace() We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.
The pop() method removes the element at the specified position.
In Javascript strings are immutable. So methods like push , pop , shift , splice don't work.
you can convert the string to a list. list(string)
and pop it, or you could iterate in chunks slicing the list []
or you can slice the string as is and iterate in chunks
To follow up on dm03514's answer, you can do something like this:
output = ""
ex = "hello"
exList = list(ex)
exList.pop(2)
for letter in exList:
output += letter
print output # Prints 'helo'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With