==============================================
String :-------->
String is a sequence which is made up of one or more UNICODE characters.
Here the character can be a letter, digit, whitespace or any other symbols.
* A string can be created by enclosing one or more characters in single, double or triple
quote.
* Every character has a unique position / index.
* There are two types on indexes. These are:
• Forward Index
• Backward Index
• Forward index starts from front as 0 for 1st element and ends with length / size – 1 for
the last element.
• Backward index starts from back as -1 for last element and ends with length / size * -1
for the first element.
Accessing Characters in a String =======>
• Each individual character in a string can be accessed using a technique called
indexing.
• The index specifies the character to be accessed in the string and is written in square
brackets ([ ]).
e.g. suppose s is string......
0 1 2 3 4 5 6
S T E P H E N
-7 -6 -5 -4 -3 -2 -1
If we want to access....
Second character s [ 1 ] or s [ -6 ]
Last character s [ 6 ] or s [ -1 ]
String is Immutable ====>
• A string is an immutable data type.
• It means that the contents of the string cannot be changed after it has been created.
St = "Hello"
St[0] = 'M'
print(St) --------> Error
Features of String : ⇒⇒
* It is immutable in nature.
* It follows insertion order.
* It follows the indexing and slicing concept.
* It can store any UNICODE character within single/double/triple quote.
String Operations ===>
Python allows certain operations on string data type, such as concatenation,repetition,
1
, membership and slicing.
* Concatenation ( + )
Concatenate means to join. Python allows us to join two strings using concatenation
operator plus which is denoted by symbol +.
Concatenation can take place only when both the operands / values are of string type.
If any value is of another type then it will result in Error.
e.g.
"cat" + "mat" -----> catmat
'5' + '3' ------> 53
'5' + 3 ------> Error
* Replication ( * )
Python allows us to repeat the given String. It can be done using repetition operator
which is denoted by symbol *
Note:-->
Replication can take place only when one of the operands/ values are of string type and the
other operand is of int
i.e. Number type value.
e.g.
'cat' * 3 --------> catcatcat
'5' * 4 ----------> 5555
3 * "7" ----------> 777
'5' * '2' ----------> Error
* Membership ( in / not in )
There are two membership operators in Python. They are in and not in. • These are used to
search for the presence of a character / sub string in another string.
They return their result in the form of True / False.
e.g.
'N' in "MANGO" ------> True
"ics" in "Phyics" -------> True
'd' in "hello' --------> False
'come' not in "welcome" ----> False
* Comparison
String are compared on the basis of lexicographical ordering.
The comparison is done on the basis of ASCII / Ordinal values. These are:
Character Ordinal Values
0–9 48 to 57
A–Z 65 to 90
a–z 97 to 122
Space 32
e.g.
if "Ram" > "Raj" ---------> True
2