Answer to Question #349396 in Python for netha

Question #349396

snake format


1
Expert's answer
2022-06-09T09:58:35-0400

The answer to your question


text_in=input("Enter text to format in snakecase: \n")
if text_in.isspace():
    print("The string contains only spaces")
else:
    text_out=text_in.lower().replace(' ', '_') 
    for ch in list([',','.','!','@','#','$','%','__']): 
        text_out=text_out.replace(ch, '')
    text_out=text_out.strip('_') 
    print(text_out)


Answer to question with comments


text_in=input("Enter text to format in snakecase: \n")
if text_in.isspace(): #Checking for an empty string
    print("The string contains only spaces")
else:
    text_out=text_in.lower().replace(' ', '_') #changes the case of characters and replaces spaces with underscores
    for ch in list([',','.','!','@','#','$','%','__']): #removes the specified characters
        text_out=text_out.replace(ch, '')
    text_out=text_out.strip('_') #Remove "_" characters at the beginning and at the end of the string
    print(text_out)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS