Strings in Python

Ankit Deshmukh
TechGannet
Published in
3 min readMay 26, 2018

Strings are the sequence of characters . We can use single or double quote to represent a string:

  1. ‘hi’
  2. “hi”
  3. “I’m playing cricket”

If your string contains single quote as mentioned above, then wrap the string in double quotes, because

‘I’m playing cricket’ — will result in error

To escape from this error, we can use escape character also:

‘I\’m playing cricket’

>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
>>> print('"Isn\'t," she said.')
"Isn't," she said.

We can use indexing and slicing to grab the sub-sections of a string. Indexing uses the [] notation after the string. You can access single character from a string.

e.g. let’s consider a string, “india”

so, index of i is 0, index of n is 1 and same index of a is 5. In short, indexing for “india” is like 0 1 2 3 4 .

Reverse indexing for string “INDIA” is -5 -4 -3 -2 -1

>>> name = 'Python'
>>> name[0]
'P'
>>> name[5]
'n'
#reverse indexing>>> name[-1]
'n'
>>> name[-6]
'P'

length of string is calculated using len() function. len() function treats white spaces as a character.

>>> name= 'Python'
>>> len(name)
6

Slicing allows you to access multiple characters of a string. Syntax for slicing a string is :

[start:stop:step]

start: numerical index for slice start

stop: index you will go upto

step: size of “jump” you take

e.g. Slicing will start from index from 0 to 7 and jumping by 2 positions.

>>> name = 'constitution'
>>> name[0:7:2]
cntt

let’s see more examples:

>>> name = 'constitution'
>>> name[3:]
'stitution'
>>> name[:5]
'const'
>>> name[:-3]
'constitut'
>>> name[-3:]
'ion'

Reverse of a string can be done as below:

>>> name[::-1]
'niotutitsnoc'

Immutability of a string:

>>> name = 'Python'
>>> name[0]='a'
Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'str' object does not support item assignment

Strings are immutable. It means you cannot change the content of a string.

You can change the content of string by means of concatenation.

Let’s say I want to change P in ‘Python’ to A to make it ‘Aython’.

>>> name = 'Python'
>>> x = name[1:]
>>> x = 'A' + x
>>> x
'Aython'
#if you run it more times then it will again append the character
>>> x = 'A' + x
>>> x = 'A' + x
>>> x
'AAAython'

Let’s explore more:

>>> x = 'Python'
>>> x*2
'PythonPython'
>>> '2' + '2'
'22'
Some in-built functions:>>> x.upper() # this won't affect original string, it's just a copy
'PYTHON'
>>> x.split('t')
['Py','hon']

String Formatting:

There are two methods for formatting:

  1. .format() method
>>> print(“my name is {}”.format('ankit'))
my name is ankit

.format() method had taken string ‘ankit’ and kept in the place of curly braces.

>>> print(“Colours are {} {} {} ".format('red','blue','yellow'))
Colours are red blue yellow
>>> print("Colours are {c} {a} {b} ".format(a='red',b='blue',c='yellow'))
Colours are yellow red blue

2. f-strings (formatted string literals) (Python 3.6 update)

>>> name='python'
>>> print(f'Hello, {name} is a programming language')
Hello, Python is a programming language

Differences in printing in Python 2 and Python 3:

Python 2:

print 'Hello World'

Python 3:

In Python 3, print is a function, not a statement.

print ('Hello World')

Happy Coding!

--

--