+ 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
20 Answers
+ 1
y = ""
x = "hello"
for i in range(len(x)):
y += x[i : i + 1]
print(y)
+ 8
Markus Kaleton any idea how to reverse it
h
he
hel
hell
hello
hell
hel
he
h
+ 8
thx Markus Kaleton
+ 5
Lemuel XD
+ 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])
+ 4
thx Lemuel
+ 4
Lemuel ohhhh
XD
+ 1
for i in range(x.length):
y += x[i _ 1 : i]
print(y)
+ 1
wait sry i forgot something
+ 1
i confused it with javascript
+ 1
god dude my notification tab is overflown
+ 1
a = "hello"
for i in range(len(a)+1):
print(a[0:i])
+ 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
+ 1
I think this is an example where i used numbers
https://code.sololearn.com/cB6oyOnnaApC/?ref=app
+ 1
Han Xuyang this is for python
+ 1
sorry my bad
0
Instead of range() and len(), you can just apply the loop on the string
for i in x:
print(i)
0
Use
for(x=0; x<5; x++){
cout << string[x] << endl;
}
0
l think it’s ok
0
well I yes, you'd learn more by trying it out yourself though