Python Training : Strings and String Methods

 

Strings

>>> a = "python training and exercises"

>>> len(a)
29

>>> max(a)
'y'

>>> min(a)
' '

>>>print( a[0:6] + ' tutorial')
'python tutorial'

>>> a = 'python '
>>> 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
>>> 's' not in a
True
>>> 't' not in a
False


>>> a = "interface eth0 is up"
>>> if 'up' in a:
	     print ("interface is up")
    else:
	     print ("interface is down")
 
Output: 
interface is up



String methods: Count , Find and Index

>>> a = "python training and exercises"
>>> a.count('n')
4

>>> a.count('n',10,20)
3

>>> a = "interface ethernet0 up, interface ethernet1 up, interface ethernet3 down"
>>> a.count('up')
2

>>> a = "python training and exercises"
>>> a.find('n')
5

>>> a.find('n',10,20)a
11

>>> 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


Split ,Join and Splitlines


>>> a="192.168.1.1"
>>> a.split('.')
['192', '168', '1', '1']

>>> a = "python training"
>>> 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
... eth1 is down
... eth2 is up
... eth3 is up'''
>>> print (a)
eth0 is up
eth1 is down
eth2 is up
eth3 is up
>>> a.splitlines()
['eth0 is up', 'eth1 is down', 'eth2 is up', 'eth3 is up']




Strip, lstrip and rstrip

>>> a = "--------------------python training---------------------"
>>> a.strip('-')
'python training'

>>> a.lstrip('-')
'python training---------------------'

>>> a.rstrip('-')
'--------------------python training'


>>> a = "             python training            "
>>> print(a)
'             python training            '
>>> a.strip()
'python training'

Replace

>>> a.replace('n','N')
'pythoN traiNiNg'

>>> a.replace('n','N',1)
'pythoN training'

Case conversion

>>> a = "python training and exercises"
>>> 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

3. Write a python program to find common characters presents in two words

4. Write a python program to check the given string is palindrome or not

5. Write a python program to convert alternate characters to capital letters

6. Write a python program  to read an IP address from stdin and check whether it is valid or not

''' 
Interface ethernet0 is up 
Interface ethernet1 is down
Interface serial0  is down
Interface serial1 is up '''

7.1 Write a python program to find out how many interface are “up” (from the above input).

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.

9. Write a python program to reverse a string without using builtin methods
	
Next chapter: Python training : Lists, Slicing examples, List methods and Tuples
Previous chapter: Python training : While loops, For loops and Range function

CONTACT

Contact us and we'll get back to you within 24 hours.

Bangalore, India

Queries?
You can connect with us on Whatsapp 8884 884 844