When do I use "%d" in a python language?

Asked

Viewed 7,855 times

4

What is "%d" for in Python programming language?

  • Related: https://answall.com/q/262184/5878

3 answers

6


The %d is a placeholder (position marker). It is used to reserve values (numbers) in a vector.

For example:

print ('%s comprou %d laranjas' % ('Mikael', 12))

The way out is:

Mikael comprou 12 laranjas

See that in the example above I also inserted the %s, that is used to reserve strings (words).

  • I got it, thank you very much!!

  • @Mikaelpereira If the answer was useful to you, you can mark it as correct using the one on the left side of the post.

5

If you’re talking about format strings, this is a placeholder for numbers. The string formatted will always be an integer, even if the parameter is a floating point number. This way differs from using %s that will always show the number in the "original form".

There are several other position markers. You can see more in the documentation.

Ex.:

nome = 'LINQ'
idade = 155.512
print('%s tem %d anos -- %s' % (nome, idade, idade))

See working on Repl.it

Exit:

LINQ is 155 years old -- 155.512

4

Let’s go in pieces and try to understand the meaning of each character of the algorithm, because all this goes through a compiler and each character, word and or symbol has its meaning.

% (Operator for string formatting)

Formats the string according to the specified format and also serves to calculate module. But in our case the character % is what marks the start of the specifier. Your Syntax is as follows: % [key] [flags] [largura] [. precisão] [tipo de duração] [tipo de conversão] % [valores]

Character d

May the character of a string 'd' or in our case, where the character % comes first and soon after the character d comes your right, this means the following: d is the type of conversion where does the signalling of the conversion type, which affects the outcome of some kind of conversion, in our case a signed integer.

An example in operation

login = input("Login:")
senha = input("Senha:");
modulo = 100 % 2

print("O usuário informado foi: %s, a senha digitada foi: %d" %(login, senha))
print("O usuário possui um percentual de 0%")
print("O módulo de 100%2 é %d" %(modulo))

Note that you used character % in many different ways:

%(var1, var2)
%s e %d
0% e 100%2

You can learn more at official language documentation.

Browser other questions tagged

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