Let's take a closer look at the string data type.
string concatenation:
String concatenation is the process of concatenating more than one string together.
firstname = "vijanthi"
lastname = "tutorial"
fullname = firstname+lastname
print(fullname)
''''
output:
vijanthitutorial
''''
Description:-
The string value vijanthi is stored in the first name variable.
The last name variable stores the tutorial string value.
full name = first name + last name What this statement does is match the vijanthi value in first name and the tutorial value in second name with the help of the plus operator. Then vijanthi tutorial value is available. The value obtained in this way is stored in a variable called full name.
Finally, the value of the full name variable is printed on the output screen using the print() function.
firstname = "vijanthi"
lastname = "tutorial"
fullname = firstname+" "+lastname
print(fullname)
''''
output:
vijanthi tutorial
''''
Description:-
fullname = firstname + ” ” +lastname Pay attention to this statement. In this statement an empty blank space (" ") is given between first name and last name variables.
Used to give space between the value of these 2 variables.
So when you print vijanthi tutorial on the output screen, a space is left between the two words.
name = "abc"
answer = name*3
print(answer)
''''
output:
abcabcabc
''''
Description:-
The value abc is stored in the variable name.
The statement name*3 prints name three times. That is, it prints the value "abc" in the name variable 3 times.
The statement name*3 repeats the value “abc” in the variable name 3 times and stores that value in the variable answer.
The value in the answer variable is printed on the output screen.
start="www"
end=".com"
print(start+".vijathitutorial"+end)
''''
output:
www.vijathitutorial.com
''''
All these are combined together with the help of plus(+) operator and displayed on the output screen.
name = "Tutorial"
print("vijanthi {value}".format(value=name))
''''
output:
Vijanthi Tutorial
''''
Description:-
The value tutorial is stored in the name variable.
In the print( ) function we have to put “vijanthi {value}” in double quotes. Here the word vijanthi is printed first. After that the value Tutorial is printed in the {value} place holder. So we get vijanthi in the output. Comes together as tutorial.
Let's see how the value Tutorial came instead of the placeholder value.
Format(value = name) is given in print() function. The value of tutorial in the variable name is stored in value. So wherever we use value, the value Tutorial is displayed.
name = "Tutorial"
print("vijanthi {}".format(name))
''''
output:
Vijanthi Tutorial
''''
Description:-
{} is an empty placeholder.
The word vijanthi in print() function is printed first. The next empty placeholder is replaced by the value tutorial.
The reason is that there is a value tutorial in the variable name inside the format method, so it returns the value tutorial instead of the empty placeholder.
String Indexing
String Indexing is where each word in the string has an index number and using this index number we can use each character in the string.
Index number should be given inside square bracket ( [] ).
String can be used in 2 ways using index number.
Positive Index number
Negative Index number
Positive Index Number:-
Initial value of Positive Index Number is 0.
The index number of the first character of a string is 0.
name = "Vijanthi Tutorial"
print(name[0])
print(name[7])
print(name[15])
''''
output:
V
i
a
''''
Description:-
The string value vijanthi tutorial is stored in the variable Name.
The print (name[0]) statement prints the letter V in the 0th position of the vijanthi tutorial.
name[7] prints the letter i in the 7th position and name[15] prints the letter 'a' in the 15th position.
You may have a doubt as to why we have given name[0], here we use the variable name because the value vijanthi tutorial is stored in the variable name. [0] is the index number.
Negative Index number:-
A negative index number starts from the last character of a string.
It always starts with -1.
name = "Vijanthi"
print(name[-8])
print(name[-1])
''''
output:
V
i
''''
Description:-
The statement name[-8] displays the letter v. Similarly, the statement name[-1] displays the letter i on the output screen.
String Slicing
String slicing is the process of extracting only a specific portion of a string from a given string.
a = "Tutorial"
print(a[0:7])
''''
output:
Tutorial
''''
Description:-
The string value tutorial is saved in the variable a.
a[0:7] In this statement 0 represents the starting index number. 7 stands for ending index number. In that case, we should have printed the entire word tutorial, but only tutorial has been printed. The word L does not print. Because when a value is given in the end position, it takes up to the value before that value.
If you want to print the whole word tutorial then you should give it a[0:8].
a = "java"
print(a[0:])
''''
output:
java
''''
Description:-
a[0:] -> In this statement I have given a value of zero at start index. But no value is given in end index. So index number starts from 0 and takes up to the last index. So the integer value “JAVA” is printed.
a = "python"
print(a[:])
''''
output:
python
''''
Description:-
a[:] No starting and ending index values are given in this statement. So it automatically takes the starting and ending index and prints the word python on the output screen.
Here only the starting index is given, since the ending index is not given, it takes up to the last index, but here the negative value is given.
a = "hello"
print(a[3:0])
''''
output:
(no output)
''''
Description:-
a[3:0] -> In this statement we slice the string in reverse. In string slicing, the string cannot be sliced in reverse. You cannot slice the string in reverse even if you give positive/negative value.
a = "javascript"
print(a[0::2])
''''
output:
jvsrp
''''
Description:-
a[0::2] Here starting index is zero. Since no value is given for end index, it also takes up to last index. Next, the value 2 represents the step argument. step value starts from 0 index and goes two by two steps and prints the value contained in it. That is why we have the value jvsrp printed.