Python Forum
String slicing and loop iteration
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String slicing and loop iteration
#1
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
Reply
#2
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.
Reply
#3
can you provided a proper code for the mentioned output. ?
@BitPython
Reply
#4
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
Reply
#5
(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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#6
My apologies @pyzyx2qwerty, I will make the edit so that it does not directly solve the problem and stop doing this in the future.
Reply
#7
(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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#8
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?
Reply
#9
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python3 string slicing Luchano55 4 533 Feb-17-2024, 09:40 AM
Last Post: Pedroski55
  Inconsistent loop iteration behavior JonWayn 2 954 Dec-10-2022, 06:49 AM
Last Post: JonWayn
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,386 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Loop through a list of string oldtrafford 4 1,421 Mar-24-2022, 05:30 PM
Last Post: deanhystad
  Loop through a list of string oldtrafford 3 1,658 Feb-15-2022, 04:42 PM
Last Post: snippsat
  loop for dynamic cut string - cleaner way? korenron 4 1,877 Nov-22-2021, 02:30 PM
Last Post: korenron
  I am trying to reverse a string using loop codinglearner 4 2,128 Sep-28-2021, 10:46 PM
Last Post: Pedroski55
  saving each iteration of a loop sgcgrif3 3 6,642 Jul-27-2021, 01:02 PM
Last Post: DeaD_EyE
  Convert string to JSON using a for loop PG_Breizh 3 2,896 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh
  String slicing divyansh 6 3,280 May-31-2020, 06:15 AM
Last Post: pyzyx3qwerty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020