Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Jul 13th, 2018 60 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. #!/usr/bin/env python
  2.  
  3. # Author: Nageswara Rao
  4. # F-strings are introduced in python 3.6. To use f-strings your python version should be >=3.6
  5.  
  6.  
  7. # Simple example using format()
  8. >>> name = 'Nageswara Rao'
  9. >>> language = 'Python'
  10. >>> old_style = 'Hai, I am {} and I am working on {}'.format(name, language)
  11. >>> print(old_style)
  12. Hai, I am Nageswara Rao and I am working on Python
  13.  
  14.  
  15. # Using f string - Simple example
  16. >>> name = 'Nageswara Rao'
  17. >>> language = 'Python'
  18. >>> with_f_string = f'Hai, I am {name} and I am working on {language}'
  19. >>> print(with_format)
  20. Hai, I am Nageswara Rao and I am working on Python
  21.  
  22. # We can do calculations also
  23. >>> text = f'3 times 9 is equals to {3 * 9}'
  24. >>> print(text)
  25. 3 times 9 is equals to 27
  26.  
  27.  
  28. # Padding made easy - With out padding
  29. >>> for n in range(0, 4):
  30. ...   text = f'The value is {n}'
  31. ...   print(text)
  32. ...
  33. The value is 0
  34. The value is 1
  35. The value is 2
  36. The value is 3
  37.  
  38. #  With padding - Leading 0 for single digits
  39. >>> for n in range(0, 12):
  40. ...   text = f'The value is {n:02}'
  41. ...   print(text)
  42. ...
  43. The value is 00
  44. The value is 01
  45. The value is 02
  46. The value is 03
  47. The value is 04
  48. The value is 05
  49. The value is 06
  50. The value is 07
  51. The value is 08
  52. The value is 09
  53. The value is 10
  54. The value is 11
  55.  
  56. # Format longer floating poing
  57. >>> from math import pi
  58. >>> formatted_pi = f'Pi equals to {pi:.4f}'
  59. >>> print(formatted_pi)
  60. Pi equals to 3.1416 # See value formatted and also rounded off
  61. >>> formatted_pi = f'Pi equals to {pi:.5f}'
  62. >>> print(formatted_pi)
  63. Pi equals to 3.14159
  64.  
  65. # Formatting date - How simple it is
  66. >>> from datetime import datetime
  67. >>> birthday = datetime(1992, 3, 5)
  68. >>> format_birthday = f'Narendra has a birthday on {birthday:%B %d, %Y}'
  69. >>> print(format_birthday)
  70. Narendra has a birthday on January 03, 1990
  71.  
  72. # f-strings parsing is smart enough to handle named unicode code points
  73. >>> ordinal, unicode = "Hello", "I'm an software engineer"
  74. >>> print(f'{ordinal}, {unicode}')
  75. Hello, I'm an software engineer
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top