#πŸ”’ String Formatting

25 messages Β· Page 1 of 1 (latest)

analog shadow
#

Question: a and b are read from input as floating-point values, representing the base and height of a triangle. Compute the triangle's area as (a*b)/2. Use f-string's = sign feature within the provided curly brackets as the replacement field to output both the expression and result.

Invalid Code: a = float(input())
b = float(input())
print(f'{a*b/2}')

Helping: I need to put a string that says (a*b)/2= within print(f'{}) without deleting it.

green ivyBOT
#

@analog shadow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

severe skiff
analog shadow
#

Well I know how to replace variables inside of them with the {} function

#

But not really

severe skiff
#

!code block will help make sure that the * doesn't turn your code into italics

green ivyBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

severe skiff
#

In f-string, you can display both the expression and the value with a =

#

!e

name = "Bob"
print(f"{name=}")
green ivyBOT
analog shadow
#

How do I write it in my code

severe skiff
analog shadow
#

Is it text within a f string?

severe skiff
#

expression is whatever formula it is

analog shadow
#

Oh

severe skiff
#

1 + 1 is an expression

#

a + b is an expression

#

The value is after the =

analog shadow
#

I see

#

@severe skiff print(f'{a*b/2=}') I still cant manage to print both the value and expression.

analog shadow
#

Wow your right

green ivyBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.