0

I am doing a parsing of a variable, which contains a string

a1
b2
c3

Now I want to get a1, b2 and c3, so I wrote a for loop

for items in string
    print items

this returns

a
1

b
2

c
3

How do I tell the for loop, that I want each value, instead than each character?

2
  • Are the strings on new line? Commented Feb 11, 2015 at 1:05
  • yes, there is a newline on each string Commented Feb 11, 2015 at 1:06

3 Answers 3

3

You want to use split() with no args to split on whitespace.

for items in string.split()
    print items
Sign up to request clarification or add additional context in comments.

Comments

2

You can use the str.splitlines method to split the string into individual lines:

>>> data = '''\
... a1
... b2
... c3
... '''
>>> for items in data.splitlines():
...     print items
...
a1
b2
c3
>>>

1 Comment

This one should be the accepted answer because string contains new lines, not whitespaces.
0

var = "a1b2c3"

for i in range(len(var)):

if var[i].isdigit():
    print var[i]

output

1

2

3

1 Comment

Not sure about this; I do not need just the number

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.