0

# Split string and find position of each digit.

I have an input of â23 34â and I want to split them into 2 separate strings so â23â and â34â. Then once they have been split I would like to be able to loop through both numbers and find the position of each digit in the numbers. Can anyone help?

8 RĂ©ponses

+ 8

Have you attempted the task? If you have any code we can inspect, it would help us figure out where you are stuck, and address that accordingly.

+ 1

So Iâve tried starting it off like this:
numbers = â23 34â
new_numbers = numbers.split()
for num in range(len(new_numbers):
But then iâm a bit stuck on whether i should be using if/elif statements to say
If number of digits is 1 then position is 0, elif number of digits is 2 then position is 0 and 1.
If that makes sense?

+ 1

str = "1234"
#splits the string into 2 separate strings.
x = str[:len(str)//2]
y = str[len(str)//2:]
#prints the location of each element.
for count,k in enumerate(x,start =0):
print(count,k)

0

You can use split() function: input().split(" ") and then may be use list comprehension

0

Yes, you can use if/elif and find the index of each digit

0

Python's enumeration function would be easier to implement than to use if/else statements.

0

Yeh i saw that from your example, thank you. Iâm just trying to use the functions im currently learning so i can get used to how they work.

0

Ben I dont know what exactly you wana do... but i think you are close...after the for you can put print(new_numbers[num],num)..
That get the number and its position