+ 6

[SOLVED] Can for loops be used on strings?

like this x = hello for y in x #some thing like that and the output is h he hel hell hello

24th Jul 2018, 1:13 AM
LONGTIE👔
LONGTIE👔 - avatar
20 Answers
+ 1
y = "" x = "hello" for i in range(len(x)): y += x[i : i + 1] print(y)
24th Jul 2018, 1:32 AM
Lemuel
Lemuel - avatar
+ 8
Markus Kaleton any idea how to reverse it h he hel hell hello hell hel he h
24th Jul 2018, 4:24 AM
LONGTIE👔
LONGTIE👔 - avatar
24th Jul 2018, 5:01 AM
LONGTIE👔
LONGTIE👔 - avatar
+ 5
24th Jul 2018, 1:47 AM
LONGTIE👔
LONGTIE👔 - avatar
+ 5
s = 'hello' for a in range(1,len(s)*2): print(s[:a if a<5 else 5-a%5]) print() for b in range(len(s)): print(s[:b]+ (' f' * (b == len(s) - 1))) for b in range(len(s)): print(s[:len(s)-b])
28th Jul 2018, 11:01 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 4
thx Lemuel
24th Jul 2018, 1:30 AM
LONGTIE👔
LONGTIE👔 - avatar
+ 4
Lemuel ohhhh XD
24th Jul 2018, 1:34 AM
LONGTIE👔
LONGTIE👔 - avatar
+ 1
for i in range(x.length): y += x[i _ 1 : i] print(y)
24th Jul 2018, 1:29 AM
Lemuel
Lemuel - avatar
+ 1
wait sry i forgot something
24th Jul 2018, 1:32 AM
Lemuel
Lemuel - avatar
+ 1
i confused it with javascript
24th Jul 2018, 1:34 AM
Lemuel
Lemuel - avatar
+ 1
god dude my notification tab is overflown
24th Jul 2018, 1:44 AM
Lemuel
Lemuel - avatar
+ 1
a = "hello" for i in range(len(a)+1): print(a[0:i])
24th Jul 2018, 4:20 AM
Markus Kaleton
Markus Kaleton - avatar
+ 1
One way is to create two for loops and break the first when len of printed content is 1 and then it goes to next loop and returns the letters
24th Jul 2018, 4:57 AM
Markus Kaleton
Markus Kaleton - avatar
+ 1
I think this is an example where i used numbers https://code.sololearn.com/cB6oyOnnaApC/?ref=app
24th Jul 2018, 4:59 AM
Markus Kaleton
Markus Kaleton - avatar
+ 1
Han Xuyang this is for python
24th Jul 2018, 6:04 PM
Lemuel
Lemuel - avatar
+ 1
sorry my bad
24th Jul 2018, 6:05 PM
Han Xuyang
Han Xuyang - avatar
0
Instead of range() and len(), you can just apply the loop on the string for i in x: print(i)
24th Jul 2018, 2:09 PM
GoodOldTimes
GoodOldTimes - avatar
0
Use for(x=0; x<5; x++){ cout << string[x] << endl; }
24th Jul 2018, 6:00 PM
Han Xuyang
Han Xuyang - avatar
0
l think it’s ok
25th Jul 2018, 7:59 PM
XUERONG ZHANG
0
well I yes, you'd learn more by trying it out yourself though
26th Jul 2018, 1:52 AM
Shola Akanni
Shola Akanni - avatar