Apa itu f di python?

But in Python 3.6 and later, you can use f-Strings instead. f-Strings, also called formatted string literals, have a more succinct syntax and can be super helpful in string formatting.

In this tutorial, you'll learn about f-strings in Python, and a few different ways you can use them to format strings.

What are f-Strings in Python?

Strings in Python are usually enclosed within double quotes (

language = "Python"
school = "freeCodeCamp"
print(f"I'm learning {language} from {school}.")
9 ) or single quotes (
#Output
I'm learning Python from freeCodeCamp.
0). To create f-strings, you only need to add an
#Output
I'm learning Python from freeCodeCamp.
1  or an
#Output
I'm learning Python from freeCodeCamp.
2 before the opening quotes of your string.

For example,
#Output
I'm learning Python from freeCodeCamp.
3 is a string whereas
#Output
I'm learning Python from freeCodeCamp.
4 is an f-String.

How to Print Variables using Python f-Strings

When using f-Strings to display variables, you only need to specify the names of the variables inside a set of curly braces

#Output
I'm learning Python from freeCodeCamp.
5. And at runtime, all variable names will be replaced with their respective values.

If you have multiple variables in the string, you need to enclose each of the variable names inside a set of curly braces.

The syntax is shown below:

f"This is an f-string {var_name} and {var_name}."

▶ Here's an example.

You have two variables,

#Output
I'm learning Python from freeCodeCamp.
6 and
#Output
I'm learning Python from freeCodeCamp.
7, enclosed in curly braces inside the f-String.

language = "Python"
school = "freeCodeCamp"
print(f"I'm learning {language} from {school}.")

Let's take a look at the output:

#Output
I'm learning Python from freeCodeCamp.

Notice how the variables

#Output
I'm learning Python from freeCodeCamp.
6 and
#Output
I'm learning Python from freeCodeCamp.
7 have been replaced with
num1 = 83
num2 = 9
print(f"The product of {num1} and {num2} is {num1 * num2}.")
0 and
num1 = 83
num2 = 9
print(f"The product of {num1} and {num2} is {num1 * num2}.")
1, respectively.

How to Evaluate Expressions with Python f-Strings

As f-Strings are evaluated at runtime, you might as well evaluate valid Python expressions on the fly.

▶ In the example below,

num1 = 83
num2 = 9
print(f"The product of {num1} and {num2} is {num1 * num2}.")
2 and
num1 = 83
num2 = 9
print(f"The product of {num1} and {num2} is {num1 * num2}.")
3 are two variables. To calculate their product, you may insert the expression
num1 = 83
num2 = 9
print(f"The product of {num1} and {num2} is {num1 * num2}.")
4 inside a set of curly braces.

num1 = 83
num2 = 9
print(f"The product of {num1} and {num2} is {num1 * num2}.")

Notice how

num1 = 83
num2 = 9
print(f"The product of {num1} and {num2} is {num1 * num2}.")
4 is replaced by the product of
num1 = 83
num2 = 9
print(f"The product of {num1} and {num2} is {num1 * num2}.")
2 and
num1 = 83
num2 = 9
print(f"The product of {num1} and {num2} is {num1 * num2}.")
3 in the output.

#Output
The product of 83 and 9 is 747.

I hope you're now able to see the pattern.

In any f-String,

num1 = 83
num2 = 9
print(f"The product of {num1} and {num2} is {num1 * num2}.")
8,
num1 = 83
num2 = 9
print(f"The product of {num1} and {num2} is {num1 * num2}.")
9 serve as placeholders for variables and expressions, and are replaced with the corresponding values at runtime.

Head over to the next section to learn more about f-Strings.

How to Use Conditionals in Python f-Strings

Let's start by reviewing Python's

#Output
The product of 83 and 9 is 747.
0 statements. The general syntax is shown below:

if condition:
  # do this if condition is True <true_block>
else:
  # do this if condition is False <false_block>

Here,

#Output
The product of 83 and 9 is 747.
1 is the expression whose truth value is checked.

  • If the
    #Output
    The product of 83 and 9 is 747.
    1 evaluates to
    #Output
    The product of 83 and 9 is 747.
    3, the statements in the if block (
    #Output
    The product of 83 and 9 is 747.
    4) are executed.
  • If the
    #Output
    The product of 83 and 9 is 747.
    1 evaluates to
    #Output
    The product of 83 and 9 is 747.
    6, the statements in the else block (
    #Output
    The product of 83 and 9 is 747.
    7) are executed.

There's a more succinct one-line equivalent to the above

#Output
The product of 83 and 9 is 747.
0 blocks. The syntax is given below:

<true_block> if <condition> else <false_block>
In the above syntax,
#Output
The product of 83 and 9 is 747.
9 is what's done when the
#Output
The product of 83 and 9 is 747.
1 is
#Output
The product of 83 and 9 is 747.
3, and
#Output
The product of 83 and 9 is 747.
7 is the statement to be executed when the condition is
#Output
The product of 83 and 9 is 747.
6.

This syntax may seem a bit different if  you haven't seen it before. If it makes things any simpler, you may read it as, "Do this

if condition:
  # do this if condition is True <true_block>
else:
  # do this if condition is False <false_block>
4
#Output
The product of 83 and 9 is 747.
1 is
#Output
The product of 83 and 9 is 747.
3;
if condition:
  # do this if condition is True <true_block>
else:
  # do this if condition is False <false_block>
7, do this".

This is often called the ternary operator in Python as it takes 3 operands in some sense – the true block, the condition under test, and the false block.

▶ Let's take a simple example using the ternary operator.

Given a number

if condition:
  # do this if condition is True <true_block>
else:
  # do this if condition is False <false_block>
8, you'd like to check if it's even. You know that a number is even if it's evenly divisible by 2. Let's use this to write our expression, as shown below:

num = 87;
print(f"Is num even? {True if num%2==0 else False}")

In the above code snippet,

  • if condition:
      # do this if condition is True <true_block>
    else:
      # do this if condition is False <false_block>
    9 is the condition.
  • If the condition is
    #Output
    The product of 83 and 9 is 747.
    3, you just return
    #Output
    The product of 83 and 9 is 747.
    3 indicating that
    if condition:
      # do this if condition is True <true_block>
    else:
      # do this if condition is False <false_block>
    8 is indeed even, and
    #Output
    The product of 83 and 9 is 747.
    6 otherwise.
#Output
Is num even? False

In the above example,

if condition:
  # do this if condition is True <true_block>
else:
  # do this if condition is False <false_block>
8 is 87, which is odd. Hence the conditional statement in the f-String is replaced with
#Output
The product of 83 and 9 is 747.
6.

How to Call Methods with Python f-Strings

So far, you've only seen how to print values of variables, evaluate expressions, and use conditionals inside f-Strings. And it's time to level up.

▶ Let's take the following example:

author = "jane smith"
print(f"This is a book by {author}.")

The above code prints out

<true_block> if <condition> else <false_block>
6

Wouldn't it be better if it prints out

<true_block> if <condition> else <false_block>
7 instead? Yes, and in Python, string methods return modified strings with the requisite changes.

The
<true_block> if <condition> else <false_block>
8 method in Python returns a new string that's formatted in the title case - the way names are usually formatted (
<true_block> if <condition> else <false_block>
9).

To print out the author's name formatted in title case, you can do the following:

  • use the
    <true_block> if <condition> else <false_block>
    8 method on the string
    num = 87;
    print(f"Is num even? {True if num%2==0 else False}")
    1,
  • store the returned string in another variable, and
  • print it using an f-String, as shown below:
language = "Python"
school = "freeCodeCamp"
print(f"I'm learning {language} from {school}.")
0

However, you can do this in just one step with f-Strings. You only need to call the

<true_block> if <condition> else <false_block>
8 method on the string
num = 87;
print(f"Is num even? {True if num%2==0 else False}")
1 inside the curly braces within the f-String.

language = "Python"
school = "freeCodeCamp"
print(f"I'm learning {language} from {school}.")
1

When the f-String is parsed at runtime,

  • the
    <true_block> if <condition> else <false_block>
    8 method is called on the string
    num = 87;
    print(f"Is num even? {True if num%2==0 else False}")
    1, and
  • the returned string that's formatted in title case is printed out.

You can verify that in the output shown below.

language = "Python"
school = "freeCodeCamp"
print(f"I'm learning {language} from {school}.")
2

You can place method calls on any valid Python object inside the curly braces, and they'll work just fine.

How to Call Functions Inside Python f-Strings

In addition to calling methods on Python objects, you can also call functions inside f-Strings. And it works very similarly to what you've seen before.

Just the way variable names are replaced by values, and expressions are replaced with the result of evaluation, function calls are replaced with the return value from the function.

▶ Let's take the function

num = 87;
print(f"Is num even? {True if num%2==0 else False}")
6 shown below:

language = "Python"
school = "freeCodeCamp"
print(f"I'm learning {language} from {school}.")
3

The above function returns

num = 87;
print(f"Is num even? {True if num%2==0 else False}")
7 if it's called with an even number as the argument. And it returns
num = 87;
print(f"Is num even? {True if num%2==0 else False}")
8 when the argument in the function call is an odd number.

▶ In the example shown below, you have an f-String that has a call to the choice function inside the curly braces.

language = "Python"
school = "freeCodeCamp"
print(f"I'm learning {language} from {school}.")
4

As the argument was an odd number (

num = 87;
print(f"Is num even? {True if num%2==0 else False}")
9), Python suggests that you learn JavaScript, as indicated below:

language = "Python"
school = "freeCodeCamp"
print(f"I'm learning {language} from {school}.")
5

If you call the function

num = 87;
print(f"Is num even? {True if num%2==0 else False}")
6 with an even number, you see that Python tells you to learn Python instead. 🙂

language = "Python"
school = "freeCodeCamp"
print(f"I'm learning {language} from {school}.")
6
language = "Python"
school = "freeCodeCamp"
print(f"I'm learning {language} from {school}.")
7

And that ends our tutorial on a happy note!

Conclusion

In this tutorial, you've learned how you can use f-Strings to:

  • print values of variables,
  • evaluate expressions,
  • call methods on other Python objects, and
  • make calls to Python functions.

Here's a post by Jessica that explains string formatting using the

language = "Python"
school = "freeCodeCamp"
print(f"I'm learning {language} from {school}.")
8 method.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT


Apa itu f di python?
Bala Priya C

I am a developer and technical writer from India. I write tutorials on all things programming and machine learning.


If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Apa itu F string Python?

Formatted string literals (juga disebut f-string) memungkinkan Anda menyertakan nilai ekspresi Python di dalam string dengan mengawali string dengan f atau F dan menulis ekspresi sebagai {expression} .

Apa fungsi print () dalam Python?

Fungsi print pada python adalah sebuah fungsi yang digunakan untuk memunculkan output yang ingin kita print pada console. Fungsi print terlihat sangat simple namun ternyata print merupakan fungsi yang paling banyak digunakan dalam sintaks python.

Apa fungsi \n pada Python?

Karakter baris baru di Python adalah \n . Dipakai untuk menandai akhir dari sebuah baris teks.

Apa itu def di Python?

Apa itu def pada Python? - Quora. def merupakan keyword yang digunakan untuk menyatakn suatu fungsi pada program python.