The most entertaining talk on strings ever!

Then came str.format...

"The {} reigns.".format(bad_guys["name"])

"Having decimated the peaceful {}, {} now deploys his merciless "
"legions to seize military control of the galaxy.".format(
    good_guys["name"], bad_guys["leader"]
)


# This is more readable
"Having decimated the peaceful {good_name}, {bad_leader} now "
"deploys his merciless legions to seize military control of the "
"galaxy.".format(
    good_name=good_guys["name"], bad_leader=bad_guys["leader"]
)

Sometimes it's better to just say

"F it"

f string in Python 3.6

f"The {bad_guys["name"]} reigns."

f"Having decimated the peaceful {good_guys["name"]}, "
f"{bad_guys["leader"]} now deploys his merciless legions "
"to seize military control of the galaxy."

F strings aren't always necessary

def walk_through_dark_alley():

    current_situation = f"uncomfortable"

    print(current_situation)

Function support

def serious(is_serious):
    if is_serious:
        return "yes, I am serious"
    else:
        return "no, I am not serious"

am_i_serious = False
"In case you were curious, {}.".format(serious(am_i_serious))

am_i_serious = True
f"In case you were curious, {serious(am_i_serious)}."

Arithmetic

number_a = 5

number_b = 3

f"yay math: {number_a * number_b * 2}!"
'yay math: 30!'

My use case

# debugging/logging

f"quick printout {var1} {var2} {var3} {var4}"

f"quick math {var1 < var2}

f"USER: {request.user.username} {request.user.date_joined}"

F strings

By m3brown

F strings

  • 748