Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent for pop on strings

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?

like image 937
Martin Flucka Avatar asked Jun 15 '12 12:06

Martin Flucka


People also ask

Can I use pop on a string?

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.

How do you pop a character in a string?

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.

What does pop () do in Python?

The pop() method removes the element at the specified position.

Can you use pop on a string Javascript?

In Javascript strings are immutable. So methods like push , pop , shift , splice don't work.


2 Answers

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

like image 58
dm03514 Avatar answered Sep 24 '22 02:09

dm03514


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'
like image 33
Colonel_Old Avatar answered Sep 21 '22 02:09

Colonel_Old