-2

I'm printing some variables, like this:

print("First name:", first_name)
print("Last name:", last_name)
print("Password:", password)

The first two are displayed just fine but the last one is like this:

    Password:
    <the password>

Which is undesirable and inconsistent with the other ones, which is why I want it to look like this:

Password:<the password>

Using end="" did not help.

Kotauskas
  • 1,239
  • 11
  • 31
  • 3
    Does `password` itself start with a linebreak? – Samwise May 10 '20 at 05:23
  • 2
    Is this the entire code? What does `password` holds? – David May 10 '20 at 05:23
  • Does this answer your question? [How to flush output of print function?](https://stackoverflow.com/questions/230751/how-to-flush-output-of-print-function) – Basile Starynkevitch May 10 '20 at 05:26
  • @Samwise no password don't start with a linebreak – Yannis Alouache May 10 '20 at 05:26
  • @DavidS no this isn't the full code, password holds a generated password created by this url : https://www.random.org/passwords/?num=1&len=10&format=html&rnd=new that i grab with bs4 – Yannis Alouache May 10 '20 at 05:28
  • @BasileStarynkevitch no sry – Yannis Alouache May 10 '20 at 05:33
  • @YannisAlouache There's absolutely, utterly and ultimately no way that the password indeed does **not** with a hidden linebreak. You might be copying a linebreak with the password (if you're copying it from somewhere), otherwise the linebreak appears somewhere along the line and you need to grab a Python debugger and walk through your code step by step, ensuring that the linebreak doesn't appear artificially. – Kotauskas May 10 '20 at 08:23

5 Answers5

0

Try to convert the password into string inside the print func

print("password:",str(password))
0

Your password variable contains a linebreak (or its string representation does).

Either of these formatting methods will print a string variable inline with no linebreak:

>>> password = "blarf"
>>> print("password:", password)
password: blarf
>>> print(f"password: {password}")
password: blarf

If the variable contains a linebreak, though, you'll get a linebreak in the output:

>>> password = "\nblarf"
>>> print("password:", password)
password:
blarf
Samwise
  • 68,105
  • 3
  • 30
  • 44
0

Try:

print("password:",password.strip())

Your password variable content likely has a linebreak before it. strip gets rid of that. Note that it also removes all whitespaces before and after the string characters in password, including before and after spaces. If you wish to only remove linebreaks from before and after, use:

print("password:",password.strip("\n"))
Ehsan
  • 12,072
  • 2
  • 20
  • 33
0

Try and remove any newline char that exist in the password string.

You can do so using the following:

first_name = "david"
last_name = "s"
password = "\n1234"
print("firstname:",first_name)
print("lastname:",last_name)
print("password:",password)
# firstname: david
# lastname: s
# password: 
# 1234


# removing the new line
password = password.replace("\n","")
print("firstname:",first_name)
print("lastname:",last_name)
print("password:",password)
# firstname: david
# lastname: s
# password: 1234

import re
re.sub("\n", "", password)
print("firstname:",first_name)
print("lastname:",last_name)
print("password:",password)
# firstname: david
# lastname: s
# password: 1234

The first option is using replace method of string object which you can read about in the following link: https://www.tutorialspoint.com/python/string_replace.htm

The second option is using sub of the re module which you can read about in the following link: https://note.nkmk.me/en/python-str-replace-translate-re-sub/

David
  • 8,113
  • 2
  • 17
  • 36
  • 1
    These methods will remove ALL `"\n"` occurrences in the `password`, even the ones in the middle of string. – Ehsan May 10 '20 at 05:42
  • @Ehsan Agree, but looking at the OP comments on every possible answer, I thought that this is what he needs; He seem to not noticing the `\n` that exist in the `password` – David May 10 '20 at 05:46
  • I did not mean to criticize your solution. Just put it there for the reader to note it. – Ehsan May 10 '20 at 05:49
  • 1
    @Ehsan I didn't think so. just explaining my reasoning – David May 10 '20 at 06:44
0

Managed to resolve the problem thanks to Samwise I used :

password.splitlines()
password = password[1]
print("password:",password)

David S method works fine too

password = password.replace("\n","")
print("password:",password)