Pythom
Pythom
>>> len(a)
29
>>> max(a)
'y'
>>> min(a)
' '
>>> b = 'tutorial'
>>> print (a + b)
python tutorial
>>> print (a * 3)
python python python
>>> print (a * 3 + b)
python python python tutorial
>>> 't' in a
True
>>> 's' in a
False
>>>if 'up' in a:
... print ("interface is up")
...else:
... print ("interface is down")
----------Output----------
interface is up
>>> a.count('n')
4
>>> a = "interface ethernet0 up, interface ethernet1 up, interface ethernet3 down"
>>> a.count('up')
2
>>> a.find('n')
5
>>> a.rfind('q')
-1
>>> a.rfind('n')
17
>>> a.find('training')
7
'''
a.index() is like a.find(), but raises “ValueError” if the string is not found
'''
>>> a.index('n')
5
>>> a.rindex('n')
17
>>> a.index('q')
ValueError: substring not found
>>> a="192.168.1.1"
>>> a.split('.')
['192', '168', '1', '1']
>>> a.split('n')
['pytho', ' trai', 'i', 'g']
>>> a.split('n',maxsplit=1)
['pytho', ' training']
>>> a.split('n',maxsplit=2)
['pytho', ' trai', 'ing']
>>> a = ['192', '168', '1', '2']
>>> b = '.'
>>> b.join(a)
'192.168.1.2'
>>> a = '''eth0 is up
... eth2 is down
... eth2 is up
... eth3 is up'''
>>> a.splitlines()
['eth0 is up', 'eth2 is down', 'eth2 is up', 'eth3 is up']
>>> a.strip('-')
'python training'
>>> a.lstrip('-')
'python training---------------------'
>>> a.rstrip('-')
'--------------------python training'
>>> print(a)
' python training '
>>> a.strip()
'python training'
String Replace
>>> a.replace('up','down')
'Interface Eth0 is down'
Case conversions
>>> a.capitalize()
'Python training and exercises'
>>> a.title()
'Python Training And Exercises'
>>> a.upper()
'PYTHON TRAINING AND EXERCISES'
>>> b = a.upper()
>>> print(b)
'PYTHON TRAINING AND EXERCISES'
>>> b.lower()
'python training and exercises'
Python Exercises
1. Write a python program find the number of characters present in a string (with
out using len())
2. Write a python program to count the number occurrences all vowels present in a
string
6. Write a python program to read an IP address from stdin and check whether it is
valid or not
'''
Interface ethernet0 is up
7.2 Write a python program print the interface names of all interfaces which are
up.
8. Write a python program to read a date (dd-mm-yyyy) and print the month name
according the month number.