Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

['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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading