4. What is a string ?
String is an Immutable data type. This means that changes cannot be made to the created string. String data type is used to store text. String can be created within single quotes, double quotes or triple quotes. Operations like indexing, slicing etc. can be performed with String. NOTE: => If you want to store text in a variable, then there is only one data type to store text. The data type is named String. The code to create a string is given below.
st = "Hello"
print(type(st))
< class 'str' >
st = "Hello" : st is a variable in which String data type is used to store text data and
String is
written in single quotes.
print(type(st)) : type function is used to find out the type of the value stored in st
variable.
1. string indexing
String indexing is a method used to find the text at a specific index number in a string. An example of string indexing is given below.
st = "Hello"
print(st[1])
H
st = "Hello" : A variable named st is created in which the value "Hello" is stored.
print(st[1]) : This print() function prints the element stored at index 1 of the st
variable.
2. String Slicing
String slicing is an important method of string slicing which is used to find the value stored in a string from a starting index number to an ending index number. For example:-
st = "Hello"
print(st[1 : 4])
it
st = "Hello" : A variable named st is created in which the value "Hello" is stored.
print(st[1 : 4]) : This print() function prints the elements stored at index 1 to index 4
of the st
variable.
3. String Concatenation
Joining one or more strings together is called String Concatenation (Method). The string concatenation method is often used when developing large programs. Below is a code for string concatenation.
st = "Hello "
name = "user"
print(st + name)
Hello user
st = "Hello " : A variable named st has been created, in which the value "Hello" has been
stored.
name = "user" : A variable named name st has been created, in which the value "user" has
been
stored. print(st + name) => This print() function adds the st variable and the name variable
together.
Features of String Data Type
1. Immutable Nature : Python mein strings poori tarah se immutable hoti
hain.Aap kisi bani hui string ke kisi ek akshar ko beech mein se badal nahi sakte hain.
2. Rich Built-in Methods : Strings ko handle karne ke liye pehle se bohot saare
functions milte hain.Jaise text ko bada karne ke liye .upper() aur chhota karne ke liye .lower()
use hota hai.
3. Unicode Support : Python 3 mein saari strings default roop se Unicode
standard follow karti hain.Iska matlab aap string mein English ke alawa Hindi, Marathi, ya
Emojis (😀) bhi store kar sakte hain.