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
  loop for dynamic cut string - cleaner way? korenron 4 246 Nov-22-2021, 02:30 PM
Last Post: korenron
  I am trying to reverse a string using loop codinglearner 4 521 Sep-28-2021, 10:46 PM
Last Post: Pedroski55
  saving each iteration of a loop sgcgrif3 3 1,220 Jul-27-2021, 01:02 PM
Last Post: DeaD_EyE
  Convert string to JSON using a for loop PG_Breizh 3 1,077 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh
  String slicing divyansh 6 1,444 May-31-2020, 06:15 AM
Last Post: pyzyx3qwerty
  string slicing help oli_action 2 940 Mar-22-2020, 09:57 AM
Last Post: oli_action
  Changing a variable's name on each iteration of a loop rix 6 33,658 Jan-03-2020, 07:06 AM
Last Post: perfringo
  Accepting strings as arguments when slicing a string? (Ziyad Yehia on Udemy) Drone4four 4 1,544 Aug-23-2019, 07:59 PM
Last Post: Drone4four
  Parallel iteration with for loop Josh_Python890 1 1,231 Jul-19-2019, 11:50 PM
Last Post: metulburr
  string slicing default value Uchikago 1 1,356 Jul-01-2019, 11:19 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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