The actual return value of the Python input() function enters strangely.

Asked 2 years ago, Updated 2 years ago, 150 views


txt=["Ghost" vegetable\n, "i\'m Groot\n"]

Suppose you enter line=input() #Ghost vegetables\n.

if(line==txt[0]):
    print("This string is the same sentence.")

I thought that if you type in input(), the görme\n, it would be the same string as the görme\n in the real txt list, but when you actually run it, the print function doesn't work and ends. Why does it work like this??

Using Cloud Idea!

python3.6 inputstream

2022-09-22 13:39

1 Answers

https://ko.wikipedia.org/wiki/%EC%9D%B4%EC%8A%A4%EC%BC%80%EC%9D%B4%ED%94%84_%EB%AC%B8%EC%9E%90

Looks like a question about escape characters. Something like a line break is a character, but it's a character that you can't really see. A special commitment is required to express using characters that show these special function characters. If you meet \ in a quoted string, you promised that it was not a character called \ but a character that represents a special character that differs depending on the following symbol. "\n" appears to be a string of two characters, but len('\n') equals 1. Because it's just one new line character.

However, if you enter \n at the prompt that appears as input(), the backslash character and n will be two characters. So, string comparison fails.


2022-09-22 13:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.