Python Tutorial: String Formatting – Advanced Operations for Dicts, Lists, Numbers, and Dates

///Python Tutorial: String Formatting – Advanced Operations for Dicts, Lists, Numbers, and Dates

Python Tutorial: String Formatting – Advanced Operations for Dicts, Lists, Numbers, and Dates

FavoriteLoadingAdd to favorites

In this Python tutorial, we will be learning how to perform some advanced string formatting operations. Formatting our strings allows us to display our information in exactly the way we would like it to be displayed. Everyone, in almost all areas of Python programming, comes across a situation where they need to format a data type in a specific way. Let’s get started.

The code from this video can be found at:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python

source

By |2020-02-09T01:43:41+00:00February 9th, 2020|Python Video Tutorials|34 Comments

34 Comments

  1. prajval singh rajput February 9, 2020 at 1:43 am - Reply

    Corey, we love your work, More Powers To You !!!

  2. Susan Amber Bruce February 9, 2020 at 1:43 am - Reply

    So how do I break up the length of a format function line?
    I tried this and it didn't have the results I expected:
    def display_rabbit(self):
    disp_age = '{0} days, {1}, months and, {2} years old'.format(*self.age)
    ## return '{} is a {} rabbit and has {} fur and {} eyes, {} is of the {} breed of rabbits.n{} is {}.'
    ## .format(self.name,
    ## self.sex,
    ## self.fur_colour,
    ## self.eye_colour,
    ## self.name,
    ## self.breed,
    ## self.name,
    ## disp_age)
    return '{0.name} is a {0.sex} rabbit and has {0.fur_colour} fur and
    {0.eye_colour} eyes,{0.name} is of the
    {0.breed} breed of rabbits.n{0.name} is '.format(paul),disp_age

    The commeted out section works.
    the last 3 lines work but not right unless it's all one long line, how do I break up a formated line like this?

  3. Susan Amber Bruce February 9, 2020 at 1:43 am - Reply

    Very helpful, you helped me with this:
    ## <- class experiment

    class Rabbit:

    num_eyes = 2 # <- Class variable

    num_legs = 4

    num_ears = 2

    num_tails = 1

    def __init__(self, name = 'Rabbit',

    fur_colour = 'Brown',

    eye_colour = 'Brown',

    age = (1,0,0),

    sex = 'unsexed',

    breed = 'unknown'):

    self.name = name # <- Instance variable

    self.fur_colour = fur_colour

    self.eye_colour = eye_colour

    self.age = age

    self.sex = sex

    self.breed = breed

    def display_rabbit(self):

    disp_age = '{0} days, {1}, months and, {2} years old'.format(*self.age)
    return '{} is a {} rabbit and has {} fur and {} eyes, {} is of the {} breed of rabbits.n{} is {}.'

    .format(self.name,

    self.sex,

    self.fur_colour,

    self.eye_colour,

    self.name,

    self.breed,

    self.name,

    disp_age)

    ################################################

    paul = Rabbit('Paul', 'white', 'pink', (3,2,1), 'male', 'Wessex')

    print(paul.display_rabbit())

    Paul is a male rabbit and has white fur and pink eyes, Paul is of the Wessex breed of rabbits.

    Paul is 3 days, 2, months and, 1 years old.

  4. SHiV_ ßöY February 9, 2020 at 1:43 am - Reply

    anybody please.. how to write this code ->
    sentence = 'my name is {name} and i am {age} years old'.format(name = 'shivam', age='21')
    in f string format.
    example = f"my name is {name}"

  5. Parsa Hosseini February 9, 2020 at 1:43 am - Reply

    So you were born on September 24 😂😍

    Am I right?
    BTW, The best channel in Python👍

  6. Faizan Hassan February 9, 2020 at 1:43 am - Reply

    Best Channel for learning Python I have come across in my 6 months of learning Python from Youtube. You cover everything so comprehensively. Thank you man. May God bless you.

  7. Niyaad Haji February 9, 2020 at 1:43 am - Reply

    your voice is like mosh!!!!!!!!!

  8. Anil Gopi February 9, 2020 at 1:43 am - Reply

    You should compile all your Youtube videos and sell it as a Udemy course. Definitely better than the other Python courses I've bought there.

  9. Marek Ragan February 9, 2020 at 1:43 am - Reply

    first_name='James'

    last_name='Bond'

    sent='My name is {1}, {0} {1}.'.format(first_name,last_name)

    print(sent)

  10. usha rani February 9, 2020 at 1:43 am - Reply

    good

  11. bloodaxe February 9, 2020 at 1:43 am - Reply

    There is literally no point in using this ultra complicated method. just use print("my name is ",name) . way way easier and no strain on the mind.

  12. Soumya Ranjan Das February 9, 2020 at 1:43 am - Reply

    Thank you Corey for your time and effort. Keep spreading the knowledge.

  13. Gustavo Metzler February 9, 2020 at 1:43 am - Reply

    GORGEOUS

  14. Joji joy February 9, 2020 at 1:43 am - Reply

    Hey awesome tutz, can you help in understanding the dictionary the trick @ 6:50, what do u mean by unpacking the dictionary?

  15. Gordon Draskic February 9, 2020 at 1:43 am - Reply

    Corey, your videos are amazing. I've been learning Python for over a year now. Tried many different courses and books but have to say that your videos are the best I've run into so far. Simple yet informative. The way you explain makes everything look simple and easy. Great work and thanks for sharing this with us.

  16. Ankit Yadav February 9, 2020 at 1:43 am - Reply

    Bro, you won my heart man what a teacher you are!!
    I wish I would have have you since my high school studies

  17. Maulik Madhavi February 9, 2020 at 1:43 am - Reply

    Thank you so much for your effort
    !

  18. abdelhakim knis February 9, 2020 at 1:43 am - Reply

    Thank you very helpfull

  19. sol Mokua February 9, 2020 at 1:43 am - Reply

    Was reading a different book and came here just to reinforce what I had learnt. Now wondering which to choose/which is better, this vs. %s, %d, and %f.

  20. Xannos P. February 9, 2020 at 1:43 am - Reply

    It's a relief to see that people don't actually memorize the documentation. Thank you for breaking that illusion/making it all seem more achievable.

  21. zian catacutan February 9, 2020 at 1:43 am - Reply

    Nice work!!!

  22. Slobodan Tajisic February 9, 2020 at 1:43 am - Reply

    Look, you lost 48576 bytes…

  23. jacstory February 9, 2020 at 1:43 am - Reply

    hi Corey Schafer,
    thanks for your good and great channel ,
    the best of the best of python channel ,
    thank you so mush ,
    please i have one question ,
    how i can make input = 77DB565C
    than result become same like that result = x5Cx56xDBx77

  24. Marco Lavazza February 9, 2020 at 1:43 am - Reply

    you saved my butt man

  25. R. Gomes February 9, 2020 at 1:43 am - Reply

    Good job!

  26. Kevin Joseph February 9, 2020 at 1:43 am - Reply

    bro you are good enough

  27. Slim Yelow February 9, 2020 at 1:43 am - Reply

    another well done topic checked off my list….

  28. Sahejeet Singh February 9, 2020 at 1:43 am - Reply

    Best tutorials on Python, Really have to say, you cover everything so comprehensively.
    Heartfelt Gratitude and Kudos to your efforts ! 🙂

  29. Takumi _ February 9, 2020 at 1:43 am - Reply

    Does anyone know what IDE is being used?

  30. harnath atmakuri February 9, 2020 at 1:43 am - Reply

    one more best video from you thank u for this

  31. Manikanta swamy Kalyani February 9, 2020 at 1:43 am - Reply

    a={'1':Corey' , 2':'Schafer'}
    How to unpack the above dictionary in order to use it in format function
    Output should print Corey Schafer

  32. daniel oghene February 9, 2020 at 1:43 am - Reply

    this guy is the BEST python teacher ever amazing

  33. yunusmile February 9, 2020 at 1:43 am - Reply

    after learning this the first thing i did was to check my birthday now i know i was born on tuesday, thumbs up if you did the same too 🙂

  34. Enissay February 9, 2020 at 1:43 am - Reply

    Could you please include a link to the full reference of the formatting parameters such as {:02}, {:.2}, … To have more details for custom formatting.

    Thank you for the amazing tutorials !

Leave A Comment

*