bool data type:
We can use this data type to
represent boolean values.
The only allowed values for
this data type are:
True and False
Internally Python represents
True as 1
and
False as 0
b=True
type(b) ------------.>bool
Example:
a=10
b=20
c=a<b
print(c)==>True
True+True==>2
True-False==>1
str datatype:
str represents String data
type.
A String is a sequence of
characters enclosed within single quotes or double quotes.
s1='Python'
s1="Python"
By using single quotes or
double quotes we cannot represent multi line string literals.
s1="Hello Learners!!
Welcome to my YouTube
Channel"
For this requirement we
should go for triple single quotes(''') or triple double
quotes(""")
s1= '''Hello Learners!!
Welcome to my YouTube
Channel '''
s1=""" Hello
Learners!!
Welcome to my YouTube
Channel """
We can also use triple
quotes to use single quote or double quote in our String.
'''MV’s Code Guide '''
"""MV’s Code Guide"""
Indexing in Python:
In Python, Strings follows
zero based index.
The index can be either positive or negative.
Positive index means forward
direction from Left to Right
Negative index means backward direction
from Right to Left
>>> s="Python"
2) >>> s[0]
3) 'P'
4) >>> s[1]
5) 'y'
6) >>> s[-1]
7) 'n'
8) >>> s[40]
IndexError: string index out
of range
0 1 2 3 4 5
P y
t h o n
-6 -5 -4 -3 -2 -1
Slicing of Strings:
slice means a piece
[ ] operator is called slice
operator, which can be used to retrieve parts of String.
Syntax: variable_name[begin:end:step]
By default begin value is 0
end value is length of string
Syntax: variable_name[begin:end:step]
By default begin value is 0
end value is length of string
step value is 1
If you don't specify anything then default values will be takes
Example:
s="Python"
If you don't specify begin , end and step value
s[::]
then, internally it will be considered as
s[0:length of string:1]
See more examples below:
If you don't specify anything then default values will be takes
Example:
s="Python"
If you don't specify begin , end and step value
s[::]
then, internally it will be considered as
s[0:length of string:1]
See more examples below:
Repetition operator in string:
we can use * multiplication operator in Python to perform repetition of strings
e.g.: s="Python"*3
e.g.: s="Python"*3
print(s)
Output: 'PythonPythonPython'
Length of string:
To check the length of the string we can use function len()
example:
s="Python"
print(len(s))
Output: 6
******************************************************
******************************************************
******************************************************
******************************************************
No comments:
Post a Comment