在输入字符串的开头添加了一个空间

发布于 2025-02-02 14:21:08 字数 415 浏览 3 评论 0原文

我正在尝试通过获取用户输入来打印一条线。 但是在打印用户输入之前有一个空白:

代码:

UserInput = input('Enter a string: ')
print("\nEntered string is:\n", UserInput)

输出:

Enter a string: I need this, but not this

Entered string is:
 I need this, but not this

理想情况下,输出应该是: 输入一个字符串:我需要这个,但不要这个,

Entered string is:
I need this, but not this

请有人让我知道吗?

I am trying to print a line by taking the user input.
But there is an empty space before the user input is printed:

Code:

UserInput = input('Enter a string: ')
print("\nEntered string is:\n", UserInput)

Output:

Enter a string: I need this, but not this

Entered string is:
 I need this, but not this

Ideally the output should have been:
Enter a string: I need this, but not this

Entered string is:
I need this, but not this

Please can someone let me know??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东京女 2025-02-09 14:21:08

逗号在用户输入字符串之前会导致额外的空间。用+符号替换打印语句中的逗号。

UserInput = input('Enter a string: ')
print("\nEntered string is:\n" + UserInput)

输出:

Enter a string: I need this, but not this

Entered string is:
I need this, but not this

The comma is causing an extra space before the user input string. Replace the comma in the print statement with + sign.

UserInput = input('Enter a string: ')
print("\nEntered string is:\n" + UserInput)

Output:

Enter a string: I need this, but not this

Entered string is:
I need this, but not this
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文