1

I have an example of f strings in textbook, it's a function whose purpose is to take a list and print the list out enumerated, I do not understand how the code is functioning but do know it works fine. I would like to understand a few things about this code:

import random
OPTIONS = ['rock', 'paper', 'scissors']

def print_options():
    print('\n'.join(f'({i}) {option.title()}' for i,option in enumerate(OPTIONS)))


print_options()

output:

(1) Rock
(2) Paper
(3) Scissors

the problem line is the body of the function. I would like to see how to modify the line but preserving the f-string method to leave out the enumeration, e.g.

desired output:

Rock
Paper
Scissors

All I can think of is:

def _print_choices():
    print('\n.join(f'({choice.title()}))' for choice in choices)

print_choices()

Which I can see from the amount of red in the editor is not even worth running.

Any ideas?

2
  • The answers below are correct, but just fyi: you don't need the f-string, the title() method or even the function definition. Just print('\n'.join(option for option in OPTIONS)) or without list comprehension or the need for \n: for option in OPTIONS: print(option). Commented Apr 26, 2020 at 14:10
  • Hi Jack, thank you for that. I just wanted to see how to use the f-string method on a simpler example so that I could understand the example in the book. You're totally right there is no need for the f-string to give the output I required it is just to help me learn f-strings as I was a bit mystified as to what the inside of the textbook's function was doing before Richard and Eric's answers. Commented Apr 26, 2020 at 14:15

2 Answers 2

2
def print_options():
    print('\n'.join(option.title() for option in OPTIONS))

# output: 
# Rock 
# Paper 
# Scissors

Since you don't want the index, the f-string and enumeration can be totally removed.

Sign up to request clarification or add additional context in comments.

4 Comments

Apologies, I did not format the desired output correctly, have fixed it now, each list element was supposed to be on a new line.
Hi Richard, I'm going to accept yours. Out of interest how would it have looked with the f_string (even though it is superfluous).I ask because I don't get why the textbook had the enumerate index part of the fstring inside so many brackets, e.g. ({I})
@Windy71 Within f-strings, to reference a name (variable or function) you need to wrap it in {} (see realpython.com/python-f-strings/#simple-syntax for more info). The additional () around those was just for print output styling purposes, i.e. (1) instead of just 1.
Ahh you star, I never got that. Thank you Richard that is a brilliant answer.
1
OPTIONS = ('Rock', 'Paper', 'Scissors')
def _print_choices(OPTIONS, sep='\n'):
    print(sep.join([f'{choice.title()}' for choice in OPTIONS]))

Output:

>>> _print_choices(OPTIONS, '\n'):
Rock
Paper
Scissors

3 Comments

Apologies, I did not format the desired output correctly, have fixed it now, each list element was supposed to be on a new line.
Try this. Also made it more dynamic (pass the choices and the separator as the arguments)
Thank you very much Eric Jin, much appreciated!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.