Simple Python problem

Hi,
So i am currently trying to solve just a line of code in python that i simply cannot figure out what it is .
It is the following:

print('{:...}'.format(20))

So i have to fill in the dots in the brackets and have the result of **20

Thanks in advance.

1 Like

Has the documentation on Python’s format function made sense?
https://docs.python.org/3/library/stdtypes.html#str.format
https://docs.python.org/3/tutorial/inputoutput.html

1 Like

not really im looking at simplet stuff still ima beginner and just need this answer because im stuck on this for a couple of days

Did you read those links? There is an example of using format()

>>> yes_votes = 42_572_654
>>> no_votes = 43_132_495
>>> percentage = yes_votes / (yes_votes + no_votes)
>>> '{:-9} YES votes  {:2.2%}'.format(yes_votes, percentage)
' 42572654 YES votes  49.67%'
1 Like

the thing i cant figure out is how to add the “**” beside the number 20

Are you supposed to put two *s? In that case you just put ** in the string you are making. If that’s supposed to be whitespace, then you need to pick :... to show 4 digits/spaces.

Yes but i only have to fill in where the dots are so that is not correct at my case

Python format() method

Actually it is impossible to print it out because 20 is not defined, 20 is the argument of the format method, more explain: the curly braces should take the variable of 20 while 20 is not defined according to the line you mentioned.

context:

  • a = variable
  • print(“The variable a is {}”.format(a))

With all my respect, this line is a part of a script :wink: or a part of the story only :sunglasses:

20 is perfectly well defined for this purpose. All OP needs to do is replace :... with the correct format for their desired printing output.

See:

print('{:-42}'.format(20))
1 Like

no it is not !! run the same line in python terminal and tell me.

This is an example:
print(“Hi Boss {}, your actual balance is {} billions $”.format(name, billions))

Could you tell me if name and billions variables are defined?

Copy and paste my exact code. It runs.

In your example, name and billions are variables. In OP’s example, he is using the value 20. format() accepts defined variables or simple numerical values.

this is the problem that was given to me exactly without any variables


it says complete the code… of the following command so the result will be **20

it just leaves a bunxh of spaces tbh and the ** are not printed

It’s supposed to leave a lot of spaces. That’s what the format :-42 means. I’m not aware of a format that prints asterisks.

ok. Please tell me what is the expected output of this exercise or what you are asked to do?

if you take a close lookyou can see the code i posted here.
It asks me to complete the blank space (…) of the following command so the result is **20.0

Am i getting something wrong?Maybe they dont need to be printed but this is the exact translation of the sentence above it

ok I am working on it :slightly_smiling_face:

It looks like you can get asterisks by print("{:*^20s}".format("Geeks"))
Though this is centered

Source: https://www.geeksforgeeks.org/python-format-function/

Ah ha! Look at this list of formatting types: https://www.w3schools.com/python/ref_string_format.asp

If you combine the correct alignment symbol with the use of *, you’ll get what you want. It looks like you may also need to use the fixed point number format.

i took a look at the list.
didnt find something including asterisks did you?