0% found this document useful (0 votes)
52 views5 pages

Pythom

This document discusses various string methods in Python including length, character access, concatenation, repetition, membership testing, splitting, joining, stripping, replacement, case conversion, and exercises. It shows examples of using len(), max(), min(), slicing, addition, multiplication, 'in' operator, if/else, count(), find(), index(), split(), join(), splitlines(), strip(), lstrip(), rstrip(), replace(), capitalize(), title(), upper(), lower() methods on strings. It also provides 9 string exercises including checking for palindromes, alternating case letters, validating IP addresses, and parsing interface status.

Uploaded by

veera v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views5 pages

Pythom

This document discusses various string methods in Python including length, character access, concatenation, repetition, membership testing, splitting, joining, stripping, replacement, case conversion, and exercises. It shows examples of using len(), max(), min(), slicing, addition, multiplication, 'in' operator, if/else, count(), find(), index(), split(), join(), splitlines(), strip(), lstrip(), rstrip(), replace(), capitalize(), title(), upper(), lower() methods on strings. It also provides 9 string exercises including checking for palindromes, alternating case letters, validating IP addresses, and parsing interface status.

Uploaded by

veera v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

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)


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
... eth2 is down
... eth2 is up
... eth3 is up'''

>>> print (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']

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'

String Replace

>>> a = "python training"

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


'pythoN traiNiNg'

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


'pythoN training'

>>> a = "Interface Eth0 is up"

>>> a.replace('up','down')
'Interface Eth0 is down'

Case conversions

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

You might also like