(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 |1 Answer
Reset to default 1When 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")
print()
puts a separator between each item that is printed; a space by default, but you can supply asep=
parameter to override that. That's the extra space you're seeing.sep=""
would get rid of it - or you could usesep="\n"
and then you wouldn't need the newlines in the individual strings. – jasonharper Commented Mar 16 at 1:10