Python Forum
String slicing and loop iteration - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: String slicing and loop iteration (/thread-27389.html)



String slicing and loop iteration - divyansh - Jun-04-2020

string="demoString"
for ch in string:
    print(ch)    # this for loop will post a single character
#current outpput
#d
#e
#m
#o
#S
#t
#r
#i
#n
#g

''' my question is how to have sub-strings in output by the same functional loop (i mean is their any way to fill ch with a sub-string instead of just a single character (same functional loop mean can we use slicing for having a the sub-string-> 
 for ch in string[::3]: # i know this will not work but i am giving you a reference for the loop 
    <expresion>)
'''
# desired output if sub string length is 3 ->
#dem
#oSt
#rin
#g



RE: String slicing and loop iteration - BitPython - Jun-04-2020

A character will be only one character. If you want more than one character, you will need to use a string.

You might want to use var = list(string) and iterate through the string.


RE: String slicing and loop iteration - divyansh - Jun-04-2020

can you provided a proper code for the mentioned output. ?
@BitPython


RE: String slicing and loop iteration - knackwurstbagel - Jun-04-2020

I experimented with this and came up with a solution that uses the step argument of range to iterate an index, and using this index in combination with iterator[<start>:<stop>] to grab the substrings. I found it handy to store the size_of_jump = 3 and use that in the calculations for the <stop> I hope this helps.

Check out this very cool document Basics of Indexing and Slicing


RE: String slicing and loop iteration - pyzyx3qwerty - Jun-05-2020

(Jun-04-2020, 11:16 PM)divyansh Wrote: can you provided a proper code for the mentioned output. ?
@BitPython

You have to figure the code on your own - the forum is focused on education, not solving answers for another person


RE: String slicing and loop iteration - knackwurstbagel - Jun-05-2020

My apologies @pyzyx2qwerty, I will make the edit so that it does not directly solve the problem and stop doing this in the future.


RE: String slicing and loop iteration - pyzyx3qwerty - Jun-06-2020

(Jun-05-2020, 10:06 PM)knackwurstbagel Wrote: My apologies @pyzyx2qwerty, I will make the edit so that it does not directly solve the problem and stop doing this in the future.
I didn't mean you, @knackwurstbagel, I meant @divyansh, as he was asking for the solution directly. You can continue explaining answers in the future, and only post your code in any section except for Homework, as long as the OP has tried and posted something


RE: String slicing and loop iteration - knackwurstbagel - Jun-06-2020

No wories @pyzyx3qwerty however when you said that noted that I had directly provided working code and felt guilty about that. Much respect. I understand what you mean. Thank you though I appreciate the clarification. I guess I'm just an apologetic Canadian eh?


RE: String slicing and loop iteration - perfringo - Jun-06-2020

As this is under 'General Coding Help' then I would suggest heading to Python documentation, specifically to Text Processing Services

Quote:The modules described in this chapter provide a wide range of string manipulation operations and other text processing services.

Particular attention could be paid to textwrap - Text wrapping and filling. Look at penultimate function at that page - this should do the trick and deliver desired result.


RE: String slicing and loop iteration - divyansh - Jun-07-2020

Quote: You have to figure the code on your own - the forum is focused on education, not solving answers for another person

Quote: My apologies @pyzyx2qwerty, I will make the edit so that it does not directly solve the problem and stop doing this in the future.

i haven't read your hints(i was busy from the last 2 days) guys but thanks i got to my solution myself by the way and
@pyzyx3qwerty i was just asking for the answer because i want to check the implementation as how good or bad my implementation is compare to others who are in python field from a while, that's it and i always try to find a solution by myself but thanks