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.
Python Notes - Strings
Get link
Facebook
Twitter
Pinterest
Email
Other Apps
By
Raaz
-
Python Notes
String Methods
string.capitalize()
Capitalized first letter of string.
string.strip()
Strips whitespaces both left and right side.
string.lstrip()
Strips whitespaces left side
string.rstrip()
Strips whitespaces right side
string.lower()
turns string into lower case
string.upper()
turns string into upper case
string.split()
Split string into array
String Formatting
Formatting using %{s,i}
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.
Today while using grep command via rundeck I was through grep: unknown device method error. And the reason is due to I am having ' - ' in my search pattern I was through this error. I mean search includes grep "-search.this" /path/to/file Then you may get error with grep. So remove ' - ' in pattern and repeat your search. grep "search.this" /path/to/file Hope that helps.
Hello, Today I am writing below article which can help you to upload files to SFTP/FTP by using CURL. Ok why we need that ? Let me tell explain!! How we login into SFTP/FTP ? [root@virt03 test]# sftp 192.168.56.110 Connecting to 192.168.56.110... root@192.168.56.110's password: sftp> ls anaconda-ks.cfg nodes post-install post-install.log sftp> exit and uploading files with put command. Its a lengthy way. So recently I have gone through few articles and with some R&D I have modified it as script and command-line argument support.So you can call the script with filename as argument. So Lets do this!!! Command 1 : This is for uploading a single to SFTP/FTP by using CURL. SFTP curl -k -u virt03:virt03 -T file4 sftp://192.168.56.110/home/virt03/ Syntax : curl -k -u username:password -T filename sftp://IP_Addreess OR Hostname:/path/to/upload FTP curl -k -u virt03:virt03 -T f
Here is a small snippet to find outgoing IP of your Linux box. 1. To find the outgoing internal IP only # ip route get 8.8.8.8 | head -1 | gawk '{ print $7 }' 2. To find the outgoing internal IP along with interface. # ip route get 8.8.8.8 | head -1 | gawk '{ print $5,$7 }' 3. In-case if your system is connected to Internet, to find the outgoing external IP # curl ifconfig.me Note: In case if your system having public IP assigned then there is no difference between outgoing internal IP and external IP (both are same).
Comments
Post a Comment