python tutorial - String Literal | Python Literals - learn python - python programming




  • Literals can be defined as a data that is given in a variable or constant.
  • Python support the following literals:

I. String literals:

  • A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" ").
  • String literals are used to represent a sequence of characters which, taken together, form a null-terminated string.
  • String literals can be formed by enclosing a text in the quotes.
  • We can use both single as well as double quotes for a String.
  • Objects are also called data structures.
  • Python comes with some built-in objects.
  • Some are used so often that Python has a quick way to make these objects, called literals.
  • The literals include the string, Unicode string, integer, float, long, list, tuple and dictionary types.

Eg:

 "Aman", '12345'
click below button to copy the code. By Python tutorial team

Types of Strings:

  • There are two types of Strings supported in Python:
    1. Single line String
    2. Multi line String

        a. Single line String - Strings that are terminated within a single line are known as Single Line Strings.

Eg:

>>> text1='hello'  
click below button to copy the code. By Python tutorial team

        b. Multi line String - A piece of text that is spread along multiple lines is known as Multiple Line String.

        • There are two ways to create Multiline Strings:

1). Adding black slash at the end of each line.

Eg:

>>> text1='hello\  
user'  
>>> text1  
'hellouser'  
>>>  
click below button to copy the code. By Python tutorial team

2). Using triple quotation marks: -

Eg:

>>> str2='''''welcome 
to 
Wikitechy'''  
>>> print str2  
welcome  
to  
Wikitechy  
>>> 
click below button to copy the code. By Python tutorial team

II. Numeric literals:

  • Numeric Literals are immutable. Numeric literals can belong to following four different numerical types.
Int(signed integers) Long(long integers) float(floating point) Complex(complex)
Numbers (can be both
positive and negative)
with no fractional part.
eg: 100
Integers of unlimited size
followed by lowercase or
uppercase L
eg: 87032845L
Real numbers with both
integer and fractional part
eg: -26.2
In the form of a+bj where
a form the real part and b
forms the imaginary part of complex number.
eg: 3.14j

III. Boolean literals:

 python literal
  • A Boolean literal can have any of the two values: True or False.

IV. Special literals.

  • Python contains one special literal i.e., None.
  • None is used to specify to that field that is not created.
  • It is also used for end of lists in Python.

Eg:

>>> val1=10  
>>> val2=None  
>>> val1  
10  
>>> val2  
>>> print val2  
None  
>>>
click below button to copy the code. By Python tutorial team

V. Literal Collections.

  • Collections such as tuples, lists and Dictionary are used in Python.

List:

  • List contain items of different data types. Lists are mutable i.e., modifiable.
  • The values stored in List are separated by commas (,) and enclosed within a square bracket ([]). We can store different type of data in a List.
  • Value stored in a List can be retrieved using the slice operator ([] and [:]).
  • The plus sign (+) is the list concatenation and asterisk (*) is the repetition operator.

Eg:

>>> list=['aman',678,20.4,'saurav']  
>>> list1=[456,'rahul']  
>>> list  
['aman', 678, 20.4, 'saurav']  
>>> list[1:3]  
[678, 20.4]  
>>> list+list1  
['aman', 678, 20.4, 'saurav', 456, 'rahul']  
>>> list1*2  
[456, 'rahul', 456, 'rahul']  
>>>  
click below button to copy the code. By Python tutorial team

Wikitechy tutorial site provides you all the learn python , python course for beginners , learn python online interactive , how to learn python coding , best online python training , python online course certification free , python training for beginners , online python course free , learn python online free course

Related Searches to Python Literals