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


Messages In This Thread
String slicing and loop iteration - by divyansh - Jun-04-2020, 11:08 PM
RE: String slicing and loop iteration - by divyansh - Jun-04-2020, 11:16 PM
RE: String slicing and loop iteration - by divyansh - Jun-07-2020, 10:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python3 string slicing Luchano55 4 611 Feb-17-2024, 09:40 AM
Last Post: Pedroski55
  Inconsistent loop iteration behavior JonWayn 2 1,003 Dec-10-2022, 06:49 AM
Last Post: JonWayn
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,565 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Loop through a list of string oldtrafford 4 1,482 Mar-24-2022, 05:30 PM
Last Post: deanhystad
  Loop through a list of string oldtrafford 3 1,707 Feb-15-2022, 04:42 PM
Last Post: snippsat
  loop for dynamic cut string - cleaner way? korenron 4 1,943 Nov-22-2021, 02:30 PM
Last Post: korenron
  I am trying to reverse a string using loop codinglearner 4 2,181 Sep-28-2021, 10:46 PM
Last Post: Pedroski55
  saving each iteration of a loop sgcgrif3 3 6,740 Jul-27-2021, 01:02 PM
Last Post: DeaD_EyE
  Convert string to JSON using a for loop PG_Breizh 3 2,979 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh
  String slicing divyansh 6 3,371 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