Python Forum
week 1 python help - string formatting ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
week 1 python help - string formatting ?
#11
(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

print("Sec290 welcomes" {}, {} "to the world of Python")'.' format (first, last) 
Reply
#12
Please see the examples i added to my last post, the {} are included inside the "" not outside of it.
Reply
#13
(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
Reply
#14
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'
Reply
#15
(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
Reply
#16
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#17
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
print('This parrot has {}, you moron. This is a {} parrot'.format('expired','dead')
Reply
#18
(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:
Reply
#19
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:

>>> 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):

>> 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.
Reply
#20
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  String formatting - tax brackets darek88 1 1,969 Aug-28-2020, 09:59 AM
Last Post: Larz60+
  Calculating the number of day of the week RbaPhoenix 3 2,433 Mar-27-2020, 09:23 PM
Last Post: Larz60+
  String formatting issues? lolatyou 2 2,039 Mar-08-2020, 06:53 AM
Last Post: michael1789
  hardest week yet: some guidence please raymond2688 25 10,541 Aug-22-2019, 05:23 PM
Last Post: ichabod801
  Trying to get the week number from a string of numbers fad3r 2 3,159 Apr-15-2018, 06:52 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020