1、sys.stdin.readline( )会将标准输入全部获取,包括末尾的'\n',input()则不会。一下例子,输入  hello

import sys

line = sys.stdin.readline()

for i in range(len(line)):

  print(line[i]+'hello')

==>

hhello
ehello
lhello
lhello
ohello

hello

import sys

line = input()

for i in range(len(line)):

  print(line[i]+'hello')

==>

hhello
ehello
lhello
lhello
ohello

要想删除sys.stdin.readline( )的换行符,可以用rstrip( )函数去掉(sys.stdin.readline( ).rstrip('\n'))

2、sys.stdin.readline( )可以指定读取用户输入的字符长度。下面例子输入  hello

import sys

line = sys.stdin.readline(3)

print(line)

==>hel

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-25
  • 2022-02-22
  • 2021-12-15
  • 2021-11-19
  • 2021-12-10
  • 2021-11-30
猜你喜欢
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-04
  • 2021-12-10
相关资源
相似解决方案