Python 3.4 - String Formatting

String formatting is super useful when working with lists, tuples, and dictionaries. It allows you to do many things like calling on information and or doing things like datetime, etc. See below:

import datetime

person = {'name': 'Matt', 'age': 38}

list_person = ['Matt', 38]

sentence = 'My name is {} and I am {} years old.'.format (person['name'], person['age'])

print(sentence)

sentence2 = 'My name is {0} and I am {1} years old.'.format (person['name'], person['age'])

print(sentence2)

tag = 'h1'
text = 'This is a header'

sentence3 = '<{0}>{1}<{0}>'.format(tag, text)

print(sentence3)

sentence4 = 'My name is {0[name]} and I am {1[age]} years old.'.format (person, person)

print(sentence4)

sentence5 = 'My name is {0[name]} and I am {0[age]} years old.'.format (person)

print(sentence5)

sentence6 = 'My name is {0[0]} and I am {0[1]} years old.'.format (list_person)

print(sentence6)

class Person():
  def __init__(self, name, age):
    self.name = name
    self.age = age
   
person1 = Person('Matt', '38')

sentence7 = 'My name is {0.name} and I am {0.age} years old.'.format (person1)

print(sentence7)

sentence8 = 'My name is {name} and I am {age} years old.'.format (name = 'Matt', age = '38')

print(sentence8)

sentence9 = 'My name is {name} and I am {age} years old.'.format (**person)

print(sentence9)

for i in range(1, 11):
  sentence10 = 'The value is {}'.format(i)
  print(sentence10) # important that this is in the loop
 
for i in range(1, 11):
  sentence11 = 'The value is {:04}'.format(i) # this will give a four digit print statement
  print(sentence11)
 

pi = 3.14159265

sentence12 = 'Pi is equal to {:.2f}'.format(pi) # this will only give two decimal places
print(sentence12)

sentence13 = '1 MB is equal to {:,} bytes'.format(1000**2)

print(sentence13)

sentence14 = '1 MB is equal to {:,.2f} bytes'.format(1000**2) # the .2f will give us two decimal places

print(sentence14)

my_date = datetime.datetime(1979, 9, 25, 7, 30,00) # I already imported datetime above

print(my_date)

sentence15 = '{}'.format(my_date)

print(sentence15)

sentence16 = '{:%B %d, %Y}'.format(my_date)

print(sentence16)

sentence17 = '{0:%B %d, %Y} fell on a {0:%A} and was the {0:%j} day of the year'.format(my_date) #the zeros are an index position

print(sentence17)


OUTPUT:

Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
   
My name is Matt and I am 38 years old.
My name is Matt and I am 38 years old.
<h1>This is a header<h1>
My name is Matt and I am 38 years old.
My name is Matt and I am 38 years old.
My name is Matt and I am 38 years old.
My name is Matt and I am 38 years old.
My name is Matt and I am 38 years old.
My name is Matt and I am 38 years old.
The value is 1
The value is 2
The value is 3
The value is 4
The value is 5
The value is 6
The value is 7
The value is 8
The value is 9
The value is 10
The value is 0001
The value is 0002
The value is 0003
The value is 0004
The value is 0005
The value is 0006
The value is 0007
The value is 0008
The value is 0009
The value is 0010
Pi is equal to 3.14
1 MB is equal to 1,000,000 bytes
1 MB is equal to 1,000,000.00 bytes
1979-09-25 07:30:00
1979-09-25 07:30:00
September 25, 1979
September 25, 1979 fell on a Tuesday and was the 268 day of the year



https://www.youtube.com/watch?v=vTX3IwquFkc

Comments

Popular posts from this blog

Python 3.4 - Caesar Cipher

UML - Use Case Diagram