Saturday, 19 March 2016

How to use Strings in Python ?


Strings are immutable sequences (cannot be changed and left-to-right order) in Python.
Both strings (in single or double quotes) are same.
>>> Hello World
>>> Hello World"

It allows to embed a quote character of the other inside a string.
>>> knights , knights


Basic String operations
len()
>>> len(‘abc’)
>>> a=‘abc’
>>> len(a)

+
Concatenation
Adding two string objects creates a new string object
>>> ‘abc’ + ‘def’
>>> a=‘Hello'
>>> b=‘World’
>>> a + b
>>> a+ ‘ ’ +b

* 
Repetition
Example: Print a line of 80 dashes
>>> print ‘-’ * 80


INDEX AND SLICE
>>> aa=“SLICEOFSPAM”
>>> aa[0], aa[-2]
(‘S’,’A’)
>>> S[1:3], S[1:], S[:-1]
(‘LI’, ‘LICEOFSPAM’, ‘SLICEOFSPA’)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.