How to print the percentage sign in Python 2/3?

Asked

Viewed 3,545 times

3

How do I use % after a markup? Ex:

print("%d texto texto 10% texto"%var_a)
  • Related: https://answall.com/q/128482/64969

2 answers

3

Just use another sign %.

a = 42
print("valor de a: %d%%" %a)

>> valor de a: 42%

3

It is not recommended by the community to use the operator % for the concatenation of strings for printing, since this operator is also used to perform the resto da divisão.

This way, always use the function format for formatting strings. There is a PEP which deals with this subject, but I do not remember which is.

a = 42
print("valor de a: {}%".format(a))
  • 2

    To leave referenced, follow the link where there is the discussion about % and format: https://answall.com/q/262184/5878

  • @Andersoncarloswoss thank you so much for the reference.

  • Actually in Python 3.6 there are the fstrings that are much better than format- and, before them, the use of format, although more flexible, did not need to be universal, after all, you have to type much more. In new Python, it’s much simpler to write f"valor de a: {a}"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.