Thursday, July 15, 2021

String And Literal In Python 3

String And Literal In Python 3

In this notebook, I will talk about basics of string and Literal in Python. Following notebook has been built using Python3.6

Let us start with Python string. String can be declared in Python using double quotes.

In [1]:
string = "I am John"
In [2]:
type(string)
Out[2]:
str

For literals, we have to use the single quotes.

In [3]:
literal = 'I am John'
In [4]:
type(literal)
Out[4]:
str

As we see both are string types.

In [5]:
literal==string
Out[5]:
True
Unicode literals in Python3

In Python3+, string,literals and unicode literals are same.

In [6]:
u'I am John' == 'I am John' == "I am John"
Out[6]:
True

We can also verify that by looking at the size of each data type.

In [7]:
import sys
In [8]:
sys.getsizeof(u'I am John')
Out[8]:
58
In [9]:
sys.getsizeof('I am John')
Out[9]:
58
In [10]:
sys.getsizeof("I am John")
Out[10]:
58
Double quotes inside Single quotes

Double quotes can be used inside single quotes

(continued...)

from Planet SciPy
read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...