Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+5 votes

Having trouble with the f-string concept as we start to combine our different "spells."

What are tricks that you use to setting up the width parameters and printing information using 'f'?When do you use the { } and what goes inside them, and where?

Thanks friends

asked in CSC201 Spring 2021 by (8 points)

3 Answers

+3 votes

f strings are super useful for making long or complicated print statements WITHOUT having to worry about separating a bunch of things with commas and quotations. basically, you pass variables into curly brackets {}, and it adds them directly into the string without needing to break it up. without them, you might have something like

print('Hello, my name is', firstName, lastName, +'! I am a', major, 'student.')

which can be done instead with f strings as

print(f'Hello, my name is {firstName} {lastName}! I am a {major} student.')

everything is in a single set of quotes, with the variables in-line inside of {}.

Then, if you want to format things within {}, you do so with a colon (:) followed by f string parameters. A list of f string parameters can be found on moodle, as StringNotesheet.pdf

The width parameter is just a number after the colon, and tells the string that variable should take up at least as much space as you specify—if it is shorter, it will fill the rest with white space.
you could do something like

print('First Name    Last Name')
print(f'{Furby:14}{Doty})

which would look like

First Name    Last Name
Furby         Doty

It filled up the extra space, because I told it to, so that everything is aligned correctly.
When using the width parameter, strings get aligned left, which means it prints the extra space AFTER the string, and numbers align right, so it prints extra space BEFORE the number.

answered by (8 points)
+1

Always great answers!!! Thank you for this

+3 votes

For width in the f-string, I figured out that it would count the length of the str + the number of space it needed to equal the width that you set. Space will be added all to the right of the str.

However, if the variable you set inside {} is a number (int, float, eval), space will be added to the left.

A note that if your str or number exceeds the width you set in {}, the code still prints the whole str but there will be no space after.

For example:

something = 'This is'
nothing = 'the explanation of width'
Num = int(10)
print(f'{something:13}{nothing:10}{Num:9}!')

The output will be:

>>>
This is      the explanation of width       10!

There are 7 indexes in "This is" (including space), so the function will create 6 more spaces to be equal to the width that you set, all on the right. The part "the explanation of width" is more than 10 indexes so it will not create more space. For the integer, the function creates 7 more space since "10" only have 2 indexes to the left of the integer.

f-string is useful if you want to use the variable inside of your print() and input() statement.

answered by (8 points)
+2 votes

Since our textbook isn't new enough to include f-strings, this short PDF mini-chapter about F-strings might be useful reading...

http://cis.bentley.edu/sandbox/wp-content/uploads/Documentation-on-f-strings.pdf

answered by (186 points)
...