最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Newline makes empty spaces in front of the second line in a set, need to remove them - Stack Overflow

programmeradmin4浏览0评论

(I'm using Python for this)

My code keeps outputting in a weird way where, after the first line in a set of two outputs, the second will have a space before it.

print('{}\n'.format(password))

print('First password: {}\n'.format (first_password),
      'Second password: {}\n'.format(second_password))

print('Number of characters in {}: {}\n'.format(first_password, x),
      'Number of characters in {}: {}'.format(second_password, y))

I've got the input in place and such, but the second line in each set has a space in front of it, which messes up the synergy. I'm fairly certain the newlines are to blame, but I don't know how to fix the issue.

You entered: Red Biggie 7

First password: Red_Biggie
 Second password: 7Biggie7

Number of characters in Red_Biggie: 10
 Number of characters in 7Biggie7: 8

I tried messing with syntax, moving the lines around, nothing worked. I tried printing them on separate lines but they were spaced out too much to be valid for what I need to do. I don't have much time left to get this done so help would be greatly appreciated!

(I'm using Python for this)

My code keeps outputting in a weird way where, after the first line in a set of two outputs, the second will have a space before it.

print('{}\n'.format(password))

print('First password: {}\n'.format (first_password),
      'Second password: {}\n'.format(second_password))

print('Number of characters in {}: {}\n'.format(first_password, x),
      'Number of characters in {}: {}'.format(second_password, y))

I've got the input in place and such, but the second line in each set has a space in front of it, which messes up the synergy. I'm fairly certain the newlines are to blame, but I don't know how to fix the issue.

You entered: Red Biggie 7

First password: Red_Biggie
 Second password: 7Biggie7

Number of characters in Red_Biggie: 10
 Number of characters in 7Biggie7: 8

I tried messing with syntax, moving the lines around, nothing worked. I tried printing them on separate lines but they were spaced out too much to be valid for what I need to do. I don't have much time left to get this done so help would be greatly appreciated!

Share Improve this question asked Mar 16 at 0:55 The TricksterThe Trickster 131 silver badge4 bronze badges 1
  • 5 print() puts a separator between each item that is printed; a space by default, but you can supply a sep= parameter to override that. That's the extra space you're seeing. sep="" would get rid of it - or you could use sep="\n" and then you wouldn't need the newlines in the individual strings. – jasonharper Commented Mar 16 at 1:10
Add a comment  | 

1 Answer 1

Reset to default 1

When you use multiple arguments in the print function, it will put a space between them:

>>> print("Hello", "world")
Hello world

As you can see, there is no space in any of the arguments, but it exists in the output.

The solution is simple. You just have to use an argument called sep that is used to tell what to put between arguments in output:

>>> print("Hello", "world", sep="")
Helloworld

In the code you provide, you used the \n character at the end of each string, so I recommend you to use it as the sep argument. So your code would become:

print('{}\n'.format(password))

print('First password: {}'.format (first_password),
      'Second password: {}\n'.format(second_password), sep="\n")

print('Number of characters in {}: {}'.format(first_password, x),
      'Number of characters in {}: {}'.format(second_password, y), sep="\n")

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论