Welcome.
This is the 5thr lesson of Python tutorial, in this lesson we deal with sequences, the second category of data types in Python.
Basically, there are 5 kinds of sequences strings, lists, tuples, sets and ranges . On the other hand, there is another division of sequences, mutable and immutable.
Here, we would discuss strings. About the others we would later.
A sequence is an object consisted of an ordered and an indexed group of item.
A string ( str for short) is any character enclosed between a pair of single or double quotation marks. Strings are sequences.
For example 'Python' and "Python" are strings. Unlike Java and C++ , in Python there is no type called char ( a char is a string has only one character).
Making a string:
To make one, you can simply use a pair of single or double quotation marks, or use the built-in function str(), in fact str() function is used to convert integers and floating point numbers to a string.
Examples.
'' an empty string ( has no character)
" " holds the whitespace character (space button from keyboard).
str("python") returns "python".
Operations on strings:
+ operator (concatenation):
s1 +s2 join two strings or more together.
" p"+"y"+"thon"="python"
* operator (repetition)
str*n repeats the string str n times
"ha"*4=" hahahaha".
in and not in operators (membership)
s1 in s2 returns True if s1 is a substring of s2, not in is the opposite.
There is no operator we can split strings with. However, there is a useful technique we can do that with, which is slicing. We will talk about soon.
Access items and slicing:
To get an item we use indexing of sequences.
Syntax: str's name [index] ,the index begins from 0 ( from the left to the right).
Examples
s="python",s[0] --> " p", s[1] --> "y" ..etc
There is another way to get string's items which is the negative index. The negative index starts counting from the right to the left , while the last ( first item at right) item has the index of -1.
Examples:
s="python", s[-1] --> "n", s[-2] --> "o" ..etc
Now. Whatever if we want to access to a substring? How do we do that?
Slicing is the solution.
Syntax like to get one item , we use square brackets [] to get a substring. The method is, string's name [start=0: end=Len(string) ].
str's name [i:j] shows a substring begins from the character with position i (included) to the character with the position j (not included).
Examples.
s=" programming", s[1:5] is "rogr", s[0:6] is "progra".
Here are main slicing usage:
s[i:j] from i ( included ) to j ( not included ).
s[i:] from i to the end.
s[:i] from the beginning to i ( i not included).
s[:] all the string.
s[-i: ] the last character with position -i to the end.
Example:
"programming" [-4: ] is "ming".
s[:-i] from the beginning to the position of -i.
Example:
"Tkinter"[:-3] is "Tkin".
We mentioned above, that there is no operator to split strings with, but slicing is very useful here.
For example: if we want to split "programming" into "program" and "ming", we will make two variables, substr1 and substr2, while:
substr1="programming"[:7] and substr2="programming"[7:].
Now, let's discuss two built-in functions.
len(iterable):
returns the size (items number) of an iterable object.
Examples:
len("python") -->6.
len("my name") -->7.
We spoke in the last lesson about the built-in functionsi nt() and float(). str() does their opposite work. It converts an integer or a floating point number or even a complwx to a string.
Examples:
str(4) --> "-4".
str(3.5) --> "3.5".
In a coming lesson we will discuss the difference between mutable and immutable. And iterable objects.
This is the 5thr lesson of Python tutorial, in this lesson we deal with sequences, the second category of data types in Python.
Basically, there are 5 kinds of sequences strings, lists, tuples, sets and ranges . On the other hand, there is another division of sequences, mutable and immutable.
Here, we would discuss strings. About the others we would later.
A sequence is an object consisted of an ordered and an indexed group of item.
A string ( str for short) is any character enclosed between a pair of single or double quotation marks. Strings are sequences.
For example 'Python' and "Python" are strings. Unlike Java and C++ , in Python there is no type called char ( a char is a string has only one character).
To make one, you can simply use a pair of single or double quotation marks, or use the built-in function str(), in fact str() function is used to convert integers and floating point numbers to a string.
Examples.
'' an empty string ( has no character)
" " holds the whitespace character (space button from keyboard).
str("python") returns "python".
Operations on strings:
+ operator (concatenation):
s1 +s2 join two strings or more together.
" p"+"y"+"thon"="python"
* operator (repetition)
str*n repeats the string str n times
"ha"*4=" hahahaha".
in and not in operators (membership)
s1 in s2 returns True if s1 is a substring of s2, not in is the opposite.
There is no operator we can split strings with. However, there is a useful technique we can do that with, which is slicing. We will talk about soon.
Access items and slicing:
To get an item we use indexing of sequences.
Syntax: str's name [index] ,the index begins from 0 ( from the left to the right).
Examples
s="python",s[0] --> " p", s[1] --> "y" ..etc
There is another way to get string's items which is the negative index. The negative index starts counting from the right to the left , while the last ( first item at right) item has the index of -1.
Examples:
s="python", s[-1] --> "n", s[-2] --> "o" ..etc
Now. Whatever if we want to access to a substring? How do we do that?
Slicing is the solution.
Syntax like to get one item , we use square brackets [] to get a substring. The method is, string's name [start=0: end=Len(string) ].
str's name [i:j] shows a substring begins from the character with position i (included) to the character with the position j (not included).
Examples.
s=" programming", s[1:5] is "rogr", s[0:6] is "progra".
Here are main slicing usage:
s[i:j] from i ( included ) to j ( not included ).
s[i:] from i to the end.
s[:i] from the beginning to i ( i not included).
s[:] all the string.
s[-i: ] the last character with position -i to the end.
Example:
"programming" [-4: ] is "ming".
s[:-i] from the beginning to the position of -i.
Example:
"Tkinter"[:-3] is "Tkin".
We mentioned above, that there is no operator to split strings with, but slicing is very useful here.
For example: if we want to split "programming" into "program" and "ming", we will make two variables, substr1 and substr2, while:
substr1="programming"[:7] and substr2="programming"[7:].
Now, let's discuss two built-in functions.
len(iterable):
returns the size (items number) of an iterable object.
Examples:
len("python") -->6.
len("my name") -->7.
We spoke in the last lesson about the built-in functionsi nt() and float(). str() does their opposite work. It converts an integer or a floating point number or even a complwx to a string.
Examples:
str(4) --> "-4".
str(3.5) --> "3.5".
In a coming lesson we will discuss the difference between mutable and immutable. And iterable objects.
No comments:
Post a Comment