Advertisement
c0d3dsk1lls

str.format CodedSkills.net

Aug 3rd, 2022 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. #========================================================================================================
  2. #-------------str.format------------------------------------------------------
  3. animal = "dog"
  4. item = "moon"
  5. text = "The {} jumped over the {}"
  6. print(text.format(animal,item))
  7. #------------------------------
  8. print("The "+animal+" Jumped over the "+item)
  9. print("The {} jumped over the {}".format(animal,item))
  10. print("The {1} jumped over the {0}".format(animal,item)) #POSITIONAL ARGUMENT
  11. #-------------------------------------------------
  12. #========================================================================================================
  13.  
  14.  
  15.  
  16. print("The {animal} jumped over the {item}".format(animal="cow",item="moon")) #KEYWORD ARGUMENT
  17. #---------------------------------------------------
  18.  
  19. name = "Coded."
  20. print("Hello, my name is {}".format(name))
  21. print("Hello, my name is {0:10} Nice to meet you".format(name))
  22. print("Hello, my name is {:^10} Nice to meet you".format(name))
  23. print("Hello, my name is {:<10} Nice to meet you".format(name))
  24. print("Hello, my name is {:>10} Nice to meet you".format(name))
  25.  
  26. #----------------------------------------------------
  27.  
  28.  
  29. number = 1000
  30. print("The number is {:.3f}".format(number))
  31. print("The number is {:,}".format(number)) #AUTOMATICALLY ADDS A COMMA TO ALL 1000s PLACES
  32. print("The number is {:b}".format(number)) #DISPLAY NUMBER AS BINARY
  33. print("The number is {:o}".format(number)) #DISPLAYS NUMBER AS OCTAL
  34. print("The number is {:x}".format(number)) #DISPLAYS NUMBER AS HEXADECIMAL
  35. print("The number is {:e}".format(number)) #DISPLAYS NUMBER AS SCIENTIFIC NOTATION
  36. #----------------------------------------------------------------------------------------------------------------
  37.  
  38. #=================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement