Defined data types
1. String
A string is a sequence of characters enclosed in quotes.
We can primarily write a string in three ways,
For one-line string…
1. Single quoted string a = 'hello world'
2. Double quoted string a = "hello world"
For multi-line string…
3. Triple quoted string a = """hello world""" or
a = '''hello world'''
Properties of string
Immutable, but it has methods to delete parts of the string, insert, replace
characters, etc.
String support accessing / indexing
The string can be slicing
In string duplicate characters are allowed
String indexing
We can access individual characters using indexing, Index starts from the left of
the string and has 0 as the starting index. The index must be an integer. We can't
use floats or other types.
Python allows negative indexing for its sequences. The negative index starts
from the right of the string and has -1 as the starting index. The index of -1 refers
to the last item, -2 to the second last item, and so on.
a= h e l l o
0 1 2 3 4
-5 -4 -3 -2 -1
Example
print (a[0]) output = h
print (a[-1]) output = o
1. String
A string is a sequence of characters enclosed in quotes.
We can primarily write a string in three ways,
For one-line string…
1. Single quoted string a = 'hello world'
2. Double quoted string a = "hello world"
For multi-line string…
3. Triple quoted string a = """hello world""" or
a = '''hello world'''
Properties of string
Immutable, but it has methods to delete parts of the string, insert, replace
characters, etc.
String support accessing / indexing
The string can be slicing
In string duplicate characters are allowed
String indexing
We can access individual characters using indexing, Index starts from the left of
the string and has 0 as the starting index. The index must be an integer. We can't
use floats or other types.
Python allows negative indexing for its sequences. The negative index starts
from the right of the string and has -1 as the starting index. The index of -1 refers
to the last item, -2 to the second last item, and so on.
a= h e l l o
0 1 2 3 4
-5 -4 -3 -2 -1
Example
print (a[0]) output = h
print (a[-1]) output = o