I have this code in Python
inputted = input("Enter in something: ")
print("Input is {0}, including the return".format(inputted))
that outputs
Enter in something: something
Input is something
, including the return
I am not sure what is happening; if I use variables that don’t depend on user input, I do not get the newline after formatting with the variable. I suspect Python might be taking in the newline as input when I hit return.
How can I make it so that the input does not include any newlines so that I may compare it to other strings/characters? (e.g. something == 'a')
You are correct – a newline is included in
inputted. To remove it, you can just callstrip("\r\n")to remove the newline from the end:This won’t cause any issues if
inputteddoes not have a newline at the end, but will remove any that are there, so you can use this whetherinputtedis user input or not.If you don’t want any newlines in the text at all, you can use
inputted.replace("\r\n", "")to remove all newlines.