Python String Variable
Python programming language:
Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way. The python language can also be used in web development; Django and Flask are the frameworks used to create web applications using Python. In Python, indentation is the main concept; if we do not follow proper indentation, the Code may not run. We can easily create an application in Python if we are familiar with indentation, Variables, Operators, loop concepts and function concepts in python language.
String in Python:
The string is a primitive data type in a python programming language; Python does not contain the character data type. The string of length is considered as the character. The string is generally known as the collection of characters enclosed within a single, double, or triple quote. The strings in Python are “immutable”; we cannot change a string once they are created. In Python, whatever data we take as input will be read as a string. In Python, string concatenation is possible; we can print the addition of two strings. In a python programming language, when we give any data as the input, it will take the input as a string to convert the given data into our required data type; we need to use the method of type conversion.
Example:
If we want to give any integer number as the input in our program, we need to use the int( ) function, which is a built-in function in a python programming language which helps in the type conversion; Python takes the input as the string with the help of the int( )function we can convert the given data into the integer data type.
Code:
#Displaying the number
print(“Enter the number:”)
#Converting the input data into integer datatype.
num = int(input( ))
#displaying the number
print(num)
#displaying the data type of the input data
print(type<num>)
Output:
Enter the number
69
integer
Syntax:
my_string=input(“Data”)
Example:
my_string=input(“Python”)
print(my_string)
Output:
Python
Indexing a string:
The indexing of a string in Python starts with zero and counts until the last character. For accessing an element in a string, we use the method of indexing.
Syntax:
my_string= “…Data…”
my_string[n]
#where n is an index of the element in the string.
Example:
my_string= “Python”
#To access the first element in the string
print(my_string[0])
#To access the second element in the string
print(my_string[1])
Output:
p
y
Slicing a string:
In Python, a string can be sliced into many parts by the indexing method. The slicing method does not affect the original string but creates a string with given indexing values.
Syntax:
my_string= “…Data….”
print( my_string[n:m] )
#where n and m are any two integers
Example:
my_string= “Python”
#Starts 2nd index to 4th index.
print(my_string[2:5])
#Starts 0th index to 3rd index.
print(my_string[0:4])
#Starts 0th index to 4th index.
Print(my_string[:5])
Output:
tho
pyth
Python
We can also use the negative indexes of the elements in the string to perform the slicing operation.
Example:
my_string= “String”
#printing the last but one two elements of the string
print(my_string[-3:-1])
#printing the first two elements of the string
Print(my_string[-6:-4])
Output:
in
st
We can also reverse a string using the negative indexing method through the slicing operation.
Example:
my_string= “String”
print(my_string[::-1])
Output:
gnirts
Variable in Python:
The variables are the same as the containers which will store the data. The python programming language has an advantage: we need to declare the variables before their creation, or we need not declare the data type while using the variable. The variable act as a name which must be given to the memory location. The variable act as the basic storage unit in a program. The variable must start with a letter or an underscore; the variable name can also contain the digits in between. The variable name is case-sensitive. That is, Code and Code are both different variables. We can assign the data to the variable with the help of the equal to (=) operator.
Example:
#Creating the variable and assigning the data
name_1 = “Jack”
name_2 = “Rose”
#Displaying the variables data
print(name_1)
print(name_2)
Output:
Jack
Rose
Here we can observe that when we use the variable name, it will be directed to the memory location where the given data is present, and then it will assign us that data. Finally, when we print the data, we can observe the data at the output.