Posts: 53
Threads: 7
Joined: Jul 2019
(Jul-08-2019, 11:18 PM)Yoriz Wrote: The format part goes right at the end of the line after all of the string stuff.
Think of the string being the whole line of text, the {} just go where you want to replace stuff in the string and in the () of format goes the stuff that replaces the {}
Below is what I got out of your statement
1 |
print ( "Sec290 welcomes" {}, {} "to the world of Python" ) '.' format (first, last)
|
Posts: 2,168
Threads: 35
Joined: Sep 2016
Please see the examples i added to my last post, the {} are included inside the "" not outside of it.
Posts: 53
Threads: 7
Joined: Jul 2019
(Jul-08-2019, 11:30 PM)Yoriz Wrote: Please see the examples i added to my last post, the {} are included inside the "" not outside of it.
Thank you I think Im done for today 5 hours on 1 line of code is just not good. I know this is basic stuff and maybe i have been looking and rewriting so much Im confusing myself.....I thank you for all the help
Posts: 2,168
Threads: 35
Joined: Sep 2016
format is a method of strings, it is used by placing a . after the string
ie 'a string'.format()
format itself takes items that will replace {} that is found inside the string
ie 'keep {} keep'.format('replace')
would become 'keep replace keep'
Posts: 53
Threads: 7
Joined: Jul 2019
(Jul-08-2019, 11:41 PM)Yoriz Wrote: format is a method of strings, it is used by placing a . after the string
ie 'a string'.format()
format itself takes items that will replace {} that is found inside the string
ie 'keep {} keep'.format('replace')
would become 'keep replace keep'
yes I actually get everything you just said. I think my problem is I dont need to replace or change any text. the assignment as follows:
Your program should ask the user for his/her first name using the Python input function.
Your program should ask the user for his/her last name using the Python input function.
Your program should create two greetings using the user’s first and last name that look like this:
SEC 290 welcomes Cameron Diaz to the World of Python, assuming the user’s name is Cameron Diaz.
The first greeting should be constructed using string concatenation.
The second greeting should be constructed using the string format method.
Display each greeting using a print statement.
The bold is what I have already figured out
Posts: 4,220
Threads: 97
Joined: Sep 2016
You want one string, followed by the format method. The curly braces go in the string. They get replaced with the parameters passed to the format method.
Posts: 1,358
Threads: 2
Joined: May 2019
Work with the whole string you want, with the values then inserted at the end. You can get lost in the quotes breaking it up like you did. More like
1 |
print ( 'This parrot has {}, you moron. This is a {} parrot' . format ( 'expired' , 'dead' )
|
Posts: 5,151
Threads: 396
Joined: Sep 2016
Jul-09-2019, 12:18 PM
(This post was last modified: Jul-09-2019, 12:18 PM by metulburr.)
(Jul-08-2019, 11:37 PM)raymond2688 Wrote: I think Im done for today 5 hours on 1 line of code is just not good. I It doesnt matter how long it takes you. The point is that you keep trying. Once you learn it you will never be confused about it again. We all had to go through that phase at one point. Everyone is here to help, that is why you keep getting responses one after the other. Some people that do not try (unlike you) their thread gets abandoned.
Attached Files
Thumbnail(s)
Recommended Tutorials:
Posts: 1,950
Threads: 8
Joined: Jun 2018
Jul-09-2019, 01:35 PM
(This post was last modified: Jul-09-2019, 01:35 PM by perfringo.)
One way to look at str.format method is that curly braces are placeholders:
- String method means, that there should be string before
.format .
- Placeholders are places where you want to insert values into string
You can insert what you want into these braces. Advantage over string concatenation is that you visually see spaces etc. You can name placholders too or use indexes:
1 2 3 4 5 6 7 8 9 10 |
>>> first = 'Eric'
>>> last = 'Idle'
>>> print ( 'Welcome {given_name} {surname} to Python forum' . format (given_name = first, surname = last))
Welcome Eric Idle to Python forum
>>> print ( 'Welcome {} {} to Python forum' . format (first, last))
Welcome Eric Idle to Python forum
>>> print ( 'Welcome {0} {1} to Python forum' . format (first, last))
Welcome Eric Idle to Python forum
>>> print ( 'Welcome {1} {0} to Python forum' . format (first, last))
Welcome Idle Eric to Python forum
|
But the 'modern' way is of course f-strings which you will (probably) learn as well (requires 3.6 <= Python):
1 2 |
>> print ( f 'Welcome {first} {last} to Python forum' )
Welcome Eric Idle to Python forum
|
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 53
Threads: 7
Joined: Jul 2019
THANK YOU THANK YOU THANK YOU. I went back over the whole thread with fresh eyes and an unfrustrated brain and it clicked. It was as simple as the concatenation string. I just needed to step back and take a break. I was so eager for this class and to learn this stuff that i took a bit to much on the first day. Thank you, everyone, you have no idea how much you guys helped.
|