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?

21st Nov 2021, 1:25 PM
Ben
+ 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.
21st Nov 2021, 1:38 PM
Hatsy Rei
+ 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?
21st Nov 2021, 3:21 PM
Ben
+ 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)
21st Nov 2021, 3:31 PM
Harrison
0
You can use split() function: input().split(" ") and then may be use list comprehension
21st Nov 2021, 2:57 PM
Neethu Nath
0
Yes, you can use if/elif and find the index of each digit
21st Nov 2021, 3:32 PM
Neethu Nath
0
Python's enumeration function would be easier to implement than to use if/else statements.
21st Nov 2021, 3:37 PM
Harrison
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.
21st Nov 2021, 3:47 PM
Ben
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
23rd Nov 2021, 3:27 AM
Cristian Baeza Jimenez