Hello , We are a group a Linux System Administrators with knowledge on Linux, VmWare ,Storage , Bash and we love to learn new things and we love sharing what we know.
In [24]: import time
In [25]: time.strftime("%H:%M:%S")
Out[25]: '21:19:12'
In [26]: # Method 1
In [27]: print("Hello %s, current time is %s" % ("Raja", time.strftime("%H:%M:%S")))
Hello Raja, current time is 21:20:22
In [28]: # Method 2
In [30]: print("Hello %(name)s, current time is %(time_now)s" % { "name" : "Raja", "time
...: _now" : time.strftime("%H:%M:%S") })
Hello Raja, current time is 21:22:19
In [31]:
Formatting Using .format()
In [31]: ## Using .format()
In [32]: "Hello {} , current time {}".format("Raja", time.strftime("%H:%M:%S"))
Out[32]: 'Hello Raja , current time 21:27:23'
In [33]: "Hello {0} , current time {1}".format("Raja", time.strftime("%H:%M:%S"))
Out[33]: 'Hello Raja , current time 21:27:38'
In [36]: "Hello {u_name} , current time {time_now}".format(u_name = "Raja", time_now = t
...: ime.strftime("%H:%M:%S"))
Out[36]: 'Hello Raja , current time 21:29:01'
In [38]: "Hello {u_name} , current time {time_now}".format(**{"u_name" : "Raja", "time_n
...: ow" : time.strftime("%H:%M:%S")})
Out[38]: 'Hello Raja , current time 21:31:32'
In [39]: ## Alignment
In [41]:
...: "{:>200}".format("Right arrow, right aligned, so 20 spaces, then aligned to rig
...: ht")
Out[41]: ' Right arrow, right aligned, so 20 spaces, then aligned to right'
In [42]:
...: "{:>200}".format("Right arrow, right aligned, so 200 spaces, then aligned to ri
...: ght")
Out[42]: ' Right arrow, right aligned, so 200 spaces, then aligned to right'
In [45]:
...: "{:^20}".format("aligned center")
Out[45]: ' aligned center '
Formatting Using F-Strings
In [46]: ### F-Strings
In [48]: def current_time() :
...: return time.strftime("%H:%M:%S")
...:
In [49]: current_time()
Out[49]: '21:39:08'
In [51]: name = "Raja"
In [52]: f'Hello {name}, currne time is {current_time()}'
Out[52]: 'Hello Raja, currne time is 21:40:03'
In [53]: f' f-strings can do eval expressions as well {2+3}'
Out[53]: ' f-strings can do eval expressions as well 5'
In [54]: f'Hello {name.lower()}'
Out[54]: 'Hello raja'
In [55]: username = "Raja"
In [56]: f'You {username=}'
Out[56]: "You username='Raja'"
In [57]:
String Slicing
In [61]: "this is a string"[5:10]
Out[61]: 'is a '
String cancatenation
In [62]: name = "Raja"
In [63]: lname = "Genupula"
In [64]: name + lname
Out[64]: 'RajaGenupula'
In [65]: name +" " + lname
Out[65]: 'Raja Genupula'
In [67]: " ".join([name, lname])
Out[67]: 'Raja Genupula'
In [68]: ", ".join([name, lname])
Out[68]: 'Raja, Genupula'
In [69]:
I hope by looking at the code and output, you can understand whats happening, if more explanation needed, please add in comments.
0 comments:
Post a Comment