我正在定义一个函数,该函数将用户提供的两个字符串连接在一起,但由sys.stdin.readline()返回的字符串包含换行符,因此我的输出看起来并不是真正意义上的连接(实际上,这个输出仍然是连接的,但在两个字符串之间有一个"\n"。)如何去掉这个换行符?
def concatString(string1, string2):
return (string1 + string2)
str_1 = sys.stdin.readline()
str_2 = sys.stdin.readline()
print( "%s" % concatString(str_1, str_2))
控制台:
hello
world
hello
world
我尝试使用read(n)读取n个字符,但它仍然会添加"\n"。
str_1 = sys.stdin.read(5) '''accepts "hello" '''
str_2 = sys.stdin.read(3) '''accepts "\n" and "wo", discards "rld" '''
控制台:
hello
world
hello
wo
input
(即在 Python 2 中使用raw_input
)? - juanpa.arrivillaga