How to display text with multiple lines with format?

Asked

Viewed 401 times

1

I’ve been trying for several lines in a row with one string along with the method .format, but I can’t.

a=input("digite algo; ")
print("""contem maiusculos:{}  contem: {}n\contem números: {}  contem
alfanumericos: {}""".format(a.isupper(),a.isnumeric(),a.isalnum()))

In this case, he’s making a mistake by being outside the scope, but I’ve tried other ways, but it always turns out I can’t use the method .format or only partially.

In this case I will still need more thread, as I will use more methods than those already listed in the example.

  • Oi Matheus! Regarding your answer/new doubt. If it is related to the reply of @Andersoncarloswoss comments there in his reply. Otherwise you can create a new question.

  • If I for example mention the author of the answer and then mention a doubt I would be breaking the rule on the creation of topics?

  • If you have a question anyone can answer. If you want you can put a comment here in Anderson’s answer to indicate the link of the other question.

  • ok thank you very much. by patience.

2 answers

5

From what I understand, you want if the output is displayed in multiple lines. As it stands, it results in the error:

Indexerror: tuple index out of range

For the simple fact that his string expects 4 values to be formatted correctly and you pass only 3. Therefore, such error would be supplied by passing the fourth parameter to format, whatever.

The problem of line break is apparently because you tried to do the line break with n\, while the line break character is \n. But using the string between three quotes, as you are doing, there will be no need to use such a character. The line break itself in the string will cause you to break the line at the exit.

Take an example:

a = input("Digite algo: ")

saida = """
Você digitou: {}
O texto possui {} caracteres
O texto é um valor numérico? {}
"""

print(saida.format(a, len(a), a.isnumeric()))

See working on Ideone | Repl.it

Output examples:

>>> Digite algo: Anderson Carlos Woss

Você digitou: Anderson Carlos Woss
O texto possui 20 caracteres
O texto é um valor numérico? False

>>> Digite algo: 2017

Você digitou: 2017
O texto possui 4 caracteres
O texto é um valor numérico? True
  • Thank you very much man for the help, helped me a lot.^^

1

Today with the python3 version you can use the fstrings as follows:

a = input("Digite algo: ")

print(f"""
Você digitou: {a}
O texto possui {len(a)} caracteres
O texto é um valor numérico? {a.isnumeric()}""")

Exit:

>>> Digite algo: Seu nome

Você digitou: Seu nome
O texto possui 8 caracteres
O texto é um valor numérico? False

>>> Digite algo: 2019

Você digitou: 2019
O texto possui 4 caracteres
O texto é um valor numérico? True

Browser other questions tagged

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