鱼C论坛

 找回密码
 立即注册
查看: 1318|回复: 5

[已解决]关于python中换行问题

[复制链接]
发表于 2021-3-3 17:25:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
各位大佬们,请问为什么用\n换行符之后第二行开头会有空格出现?

  1. task = input()
  2. t = task.split(',')
  3. n = len(t)
  4. if n%2==0:
  5.     print(f'{t[0]},{t[1]}\n{t[2]},{t[3]}')
  6. else:
  7.     print(f'{t[0]},{t[1]},{t[2]}\n{t[2]},{t[3]},{t[4]}')
复制代码



输入   defrost the fridge, return books to the library, boil sausages, walk the dog, buy tickets  之后结果是这个:
defrost the fridge, return books to the library, boil sausages↩
boil sausages, walk the dog, buy tickets

就是在boil 之前有个空格。
最佳答案
2021-3-3 17:36:50
因为你输入的时候b前面就有空格啊,代码例加一行打印
  1. task = input()
  2. t = task.split(',')
  3. print(t)  #加一行打印
  4. n = len(t)
  5. if n%2==0:
  6.     print(f'{t[0]},{t[1]}\n{t[2]},{t[3]}')
  7. else:
  8.     print(f'{t[0]},{t[1]},{t[2]}\n{t[2]},{t[3]},{t[4]}')
复制代码

从结果里很明显的可以看出来
  1. defrost the fridge, return books to the library, boil sausages, walk the dog, buy tickets
  2. ['defrost the fridge', ' return books to the library', ' boil sausages', ' walk the dog', ' buy tickets ']
  3. defrost the fridge, return books to the library, boil sausages
  4. boil sausages, walk the dog, buy tickets
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-3 17:36:50 | 显示全部楼层    本楼为最佳答案   
因为你输入的时候b前面就有空格啊,代码例加一行打印
  1. task = input()
  2. t = task.split(',')
  3. print(t)  #加一行打印
  4. n = len(t)
  5. if n%2==0:
  6.     print(f'{t[0]},{t[1]}\n{t[2]},{t[3]}')
  7. else:
  8.     print(f'{t[0]},{t[1]},{t[2]}\n{t[2]},{t[3]},{t[4]}')
复制代码

从结果里很明显的可以看出来
  1. defrost the fridge, return books to the library, boil sausages, walk the dog, buy tickets
  2. ['defrost the fridge', ' return books to the library', ' boil sausages', ' walk the dog', ' buy tickets ']
  3. defrost the fridge, return books to the library, boil sausages
  4. boil sausages, walk the dog, buy tickets
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-3 17:37:47 | 显示全部楼层
你看
屏幕截图 2021-03-03 173735.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-3 17:46:02 | 显示全部楼层
昨非 发表于 2021-3-3 17:36
因为你输入的时候b前面就有空格啊,代码例加一行打印

从结果里很明显的可以看出来

我说怎么一直不对。那请问该怎么让结果没有那个空格啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-3 17:47:51 | 显示全部楼层
木水盗人 发表于 2021-3-3 17:46
我说怎么一直不对。那请问该怎么让结果没有那个空格啊

输入的时候别加空格就好了啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-3 18:36:38 | 显示全部楼层
但是题目需要没有空格
QQ图片20210303175320.png
90.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 14:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表