Slipt in python superfluous one character " "

I’m newer to Python and When I use .slipt() superfluous one character

txt = "8/"

x = txt.split("/")

print(x)

result is:

['8', '']

but I want the result is:

['8']

how to fix it

>Solution :

You are getting this because .slipt() Method split a string into a list. Using /, as a separator, split a string into a list with the respect to "/".
In the asked question:

txt = "8/"

x = txt.split("/")

print(x)

You are split the string with respect to "/". You can visualize txt = "8/" as txt = "8"+"/"+"".
If you want your output to do this ['8']. You can use x.remove("") to remove ""
so the Final Code is:

txt = "8"+"/"+""

x = txt.split("/")
x.remove("")

print(x)

Leave a Reply