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 Answers
+ 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