Here I will talk about string data type in python, so first
of all I will shear with you guys that what is string, types of sting, usage of
citation and double citation in string.
String:
String is an important and interesting data type in python
it is collection of characters that we use them a lot for saying something for
user. we cannot perform mathematics operations on them because python knows
anything among a String as symbol even a number inside it but with the
specialties of python we can use numbers as real number that I will show you in
here example later, here I can remind that we have three kinds of string Normal
string, numerical string and hybrid string that I will talk about them just in
a second.
1: Normal String:
Normal string is a string with no numbers in it that means
when we have no numbers among our character in a string that string is a normal
string like:
Name = “Zaki!”
We cannot change this kind of string to numerical datatypes.
2: Numerical String:
A string only with numbers in it is called numerical string
that means a string that every element of that is a number like:
Number = “123”
We can change this kind of string to numerical data types
and then we can perform mathematics operations on that. Example of changing a
string to a numerical data type:
Int(Number)
3: Hybrid String:
Hybrid strings are string with both numbers and symbols (it
consist of symbols and alphabet because alphabets are symbols too) in it that
means a string that it’s elements are numbers and symbols like:
Mix = “00Zaki!00”
We cannot change hybrid string to numerical data types.
Now that we know about types of string it is better that we
know about citations that is always used with string. We have two kinds of
citations for string that are single citations and double citation we can use
both of them to put the value of string in it but we can use them in different
ways like when we use double citations when we use a single citation among our
text in the value part by this way python knows that single citation among the
value not shows the end of string value but the single citation is the part of
our text like:
Info = “I am a boy and it’s good for me”
We can use the same for single citation we can use that when
we use double citation in our text like:
Info = ‘my name is Zaki and my last name is “Ahmadi”’
But if we have a string text with no single citation and
double citation in it we can use any of the citations that we like.
The next important thing about string is this that strings
are iterable that means that we can iterate on them like every element in a
sting have an address that starts from zero this addresses in programming are
known as index like below example:
Name = ‘’ Z a k i
”
Index = 0 1 2 3
In the above example I put some space among each character
of my name to show you it’s index but remember if we put a space among two
characters the space is also known as a character that in this cases we said
every character have an index.
Now that we know about indexing of string we have to know how
to use them right?
We can choose a specific character of a text in string by
use of their index we can use from index in [] brackets like:
Name = ‘Ahmad Zaki “Ahmadi”’
Name [1]
In here the result will be ‘h’ because the index of ‘h’ is
number 1 you know why? Because indexes start from 0 we can choose any element
from our string by its index by the above way we can also use these indexes by
another way we can give start index and end index to select a proper part of a
text for this we use brackets again like:
Name = ‘Ahmad Zaki “Ahmadi”’
Name [6:10]
In here I selected ‘Zaki’ from my complete name and it is
the place that I mention that the ending index will not include in result
that’s why I choose 10 for ending index.
Now if we want steps in the selected part it is so easy we
can say that for python by putting another number after ending index of course
I have to say that default step value is one. Example of steps in a selected
part of text:
Name = ‘Ahmad Zaki “Ahmadi”’
Name [0:-1:2]
In here the result will be 'AmdZk Amd' now you may think
what I mean by -1 in ending index I used that in here for you to show you
another way of indexing we can index a string from last element which starts
from -1 like:
Name = ‘’ Z a k i
”
Index = 0 1
2 3
Index = -4 -3 -2
-1
In here I used from both kind of indexing to select all of
my content in my string. that was a lot of information about string try them all
by yourself and see what how simple it is. And of course if you faced with a
problem feel free to ask it I hope that be useful for you.
No comments