Python string format 💬

preview_player
Показать описание
Python string format method tutorial explained

#Python #string #format #method

––––––––––––––––––––––––––––––
Creative Commons — Attribution-ShareAlike 3.0 Unported— CC BY-SA 3.0
––––––––––––––––––––––––––––––
Рекомендации по теме
Комментарии
Автор

# str.format() = optional method that gives users
# more control when displaying output

animal = "cow"
item = "moon"

print("The "+animal+" jumped over the "+item)

# {} = format field
print("The {} jumped over the {}".format("cow", "moon"))
print("The {0} jumped over the {1}".format(animal, item)) # positional arguments
print("The {animal} jumped over the {item}".format(animal="cow", item="moon")) # keyword arguments

text = "The {} jumped over the {}"
print(text.format("cow", "moon"))

name = "Bro"

print("My name is {}".format(name))
print("My name is {:10}".format(name, name)) # amount of padding
print("My name is {:<10}".format(name, name)) # < = left align
print("My name is {:>10}".format(name, name)) # > = right align
print("My name is {:^10}".format(name, name)) # ^ = center align


# str.format() = optional method that gives users
# more control when displaying output

number = 1000

print("The number pi is {:.3f}".format(number))
print("The number is {:, }".format(number))
print("The number is {:b}".format(number))
print("The number is {:o}".format(number))
print("The number is {:X}".format(number))
print("The number is {:E}".format(number))

BroCodez
Автор

I didn't understood this first when my Proffessor taught me but you made it easy for me. Thanks Bro!!!

samirkarki
Автор

Very great content, and well explain for new programmer. Thanks Bro Code

khiemdo
Автор

Thanks bro! This made my life so much easier!

raven
Автор

This video was very helpful. It was very concise and comprehensive (to my knowledge).

davidchilton
Автор

You are doing well and you have a great explications

quick_information
Автор

First time watching your video, really great content. Thanks for sharing Bro

hhznfge
Автор

You're are great I understand it very smoothly 🌹🌹🌹

imadiman
Автор

bro i just discover u and this is awesome im on my start with python and this is just what i need simple and easy and useful....great job and hope u r still arround im going to check the rest of ur channel and subscribe
thanks

tihomirsmoljak
Автор

good to have as a reference when you are reviewing someone else's code that uses .format()
however, just use f-strings as it is superior in everyway, readability, easier, and faster

sehunhan
Автор

what about adding blank spaces to numbers?

hailsnover
Автор

Really, really appreciate these easy to follow instructions. I am hoping that we will get some chance to put all of these together and get to use these pieces. Great. Thanks again.

howardsamuels