formatting.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. person = {'name': 'Jenn', 'age': 23}
  2. # sentence = 'My name is ' + person['name'] + ' and I am ' + str(person['age']) + ' years old.'
  3. # print(sentence)
  4. # sentence = 'My name is {} and I am {} years old.'.format(person['name'], person['age'])
  5. # print(sentence)
  6. # sentence = 'My name is {0} and I am {1} years old.'.format(person['name'], person['age'])
  7. # print(sentence)
  8. # tag = 'h1'
  9. # text = 'This is a headline'
  10. # sentence = '<{0}>{1}</{0}>'.format(tag, text)
  11. # print(sentence)
  12. sentence = 'My name is {0} and I am {1} years old.'.format(person['name'], person['age'])
  13. print(sentence)
  14. class Person():
  15. def __init__(self, name, age):
  16. self.name = name
  17. self.age = age
  18. p1 = Person('Jack', '33')
  19. sentence = 'My name is {0.name} and I am {0.age} years old.'.format(p1)
  20. print(sentence)
  21. # sentence = 'My name is {name} and I am {age} years old.'.format(name='Jenn', age='30')
  22. # print(sentence)
  23. # sentence = 'My name is {name} and I am {age} years old.'.format(**person)
  24. # print(sentence)
  25. # for i in range(1, 11):
  26. # sentence = 'The value is {}'.format(i)
  27. # print(sentence)
  28. # pi = 3.14159265
  29. # sentence = 'Pi is equal to {}'.format(pi)
  30. # print(sentence)
  31. sentence = '1 MB is equal to {} bytes'.format(1000**2)
  32. print(sentence)
  33. import datetime
  34. my_date = datetime.datetime(2016, 9, 24, 12, 30, 45)
  35. # print(my_date)
  36. # March 01, 2016
  37. sentence = '{:%B %d, %Y}'.format(my_date)
  38. print(sentence)
  39. # March 01, 2016 fell on a Tuesday and was the 061 day of the year.
  40. sentence = '{:%B %d, %Y} fell on a {} and was the {} day of the year'.format(my_date)
  41. print(sentence)