Skip to content Skip to sidebar Skip to footer

Splitting Strings In Half, Python

I have this string my_string = '717460881855742062' how can I split it in half? The string is auto-generated so just splitting by the 1 won't work

Solution 1:

You can try do this way:

firsthalf, secondhalf = my_string[:len(my_string)//2], my_string[len(my_string)//2:]

Solution 2:

Something like this should do the job:

my_string = '717460881855742062'length = int(len(my_string) / 2)
first_part = my_string[:length]
second_part = my_string[length:]

print(first_part)
print(second_part)

output:

717460881

855742062

You can modify it and make sure you take also handle the situation where the length%2 is not 0.

Post a Comment for "Splitting Strings In Half, Python"