It is a collection of characters [alphabets, digits or special characters ]including spaces that is enclosed in single quotes or double quotes or triple quotes.Ex- "Hello Students", "1412" etc.Strings are immutable, means that the contents of the string cannot be changed after it is created.
It is a process in which each character of string has its index or can be accessed using its index. The first character in the string has index 0, the next has index 1, and so on. The index of the last character will be the length of the string minus one (Forward indexing).
There is another way (Backward indexing) for indexing from last character (index = -1) to first character (index = -length of the string)
(1) Concatenation operator (+) The Concatenation operator adds or concat two strings.
ex: ->>>Str1= "Welcome"
>>>Str2 = "to srv"
>>>print(Str1+Str2)
"Welcome to srv"
(2) Replication/repetition operator (*) The multiplication operator acts as a replication operator when we have one string and one integer as operands.
ex: - >>>print("KVS"*3)
"KVSKVSKVS"
>>>print("3" *"3")
error
(3) Membership Operators: - IN and NOT IN are two membership operators to find the appearance of a substring inside the string.
IN- Returns True if a character or a substring exists in the given string; otherwise, False.
ex: - >>>"N" in "KVS"
False
>>>"V" in "KVS"
True
NOT IN- Returns True if a character or a substring does not exist in the given string otherwise, False
ex: - >>>"N" not in "KVS"
True
>>>"V" not in "KVS"
False
(4) String Slicing: - Extracting a subpart from a main string is called slicing. It is done by using a range of indices.
Syntax - string_name[Start:Stop:Step]
ex: - >>>Str = "Students"
>>>Str[0:8]
"Students"
>>>Str [ :8]
"Students"
>>>Str[0:3]
"Stu"
>>>Str[2:5]
"ude"
>>>Str[-6:-2]
"uden"
>>>Str[4:-1]
"ent"
>>>Str[1:8:2]
"tdns"
(5) Traversing: - Traversing means iterating through the elements of string, one character at a time.
ex: - >>>Str="Students"
>>> for a in Str:
print(a,"*", end='')
S * t * u * d * e * n * t * s *
>>>for a in Str:
print(a,’*’)
S *
t *
u *
d *
e *
n *
t *
s *