filmov
tv
String Data Type in Python || Lesson 17 || Python || Learning Monkey ||

Показать описание
#python#learningmonkey#pythoncoding#placements#pythontutorials#pythoncourse#pythonforbeginners#pythonfordatascience#pythonfullcourse#pythonprogramming#pythonfreecourse
String Data Type in Python
In this class, we discuss string data type in python.
String Data Type
The string is a sequence of Unicode characters. What’s Unicode we discuss in our following classes.
Let’s take an example and understand strings.
a=” Hello World.”
The above example variable a is having value Hello World.
Variable a is of string data type. Because it is having a sequence of characters.
The series of characters is given in double quotes. we can provide them in single quotes.
, is considered as a character. And space is a character.
We discuss all the characters in our coming classes.
We can use three quotes when the string is in multi-line. The example is shown below.
Memory Allocation to Strings
From the above diagram, we can observe.
Each character is assigned a memory space, and it is contiguous memory.
Total, we have eleven characters.So the index is 0,1,2,…10.
In python, it will give negative indexes. Shown in the above diagram.
Negative indexes start from the last character to the first. starting from -1,-2,-3…
Accessing Characters from the String
We use square brackets to access the characters from the string.
Example:
The above example displays the character present in the first position. Character e is present in position 1.
String Operations
String slicing:
Slicing means accessing a part of the string.
position.
Similar to the range function we discussed previously.
Examples of Negative Indexing.
The first print statement displays the character present in the -1 index position. So character d is shown.
The output displayed from starting position to -5. Hello is our output.
The last print statement will display characters from the -5 to -1 position. ie -5,-4,-3,-2 positions.
+ operator
we call + concatenation operator.
we use a space character string concatenated between strings. example d=a+” “+b.
star operator
Star operator is used to repeating the string.
Example:
Membership operators
The above example returns True. Because character h present in string a. So it returns true.
Not in works precisely opposite of in operator.
Format operator %
String Methods
Capitalize Method:
This method is used to place capital character at the starting of the string.
Example:
lower method:
The lower method is used to lower case the characters.
upper method:
The upper method is used to upper case the characters.
count method:
The count method is used to count the number of occurrences of a given string.
where the search should start.
From index position 3. the count method starts to look for character l.
The count method will search in the positions given 3,4,5,6. Last position not considered.
find method:
find method is used to identify at what position the required string occurred in a given string.
Let’s take an example for easy understanding.
It will find only the first occurrence. even l occurred three times.
Same as the count method, we can give start and end positions. That works similarly to the count method.
Find method will return the value -1. If the given string has not occurred.
len function:
The len function is used to identify the length of the string.
a=” hello world.”
Split method:
Example:
Whenever it finds the space character, it will split.
Whenever it encounters # it will split the string.
join method:
join method used to join a sequence of strings.
For now, we understand the method to join.
separator=”#”
variable l is having a list of strings.
A list of strings is joined using the join method by taking a separator #.
isalpha method:
isalpha method is used to check the given string consist of only alphabets.
Let’s take an example.
will display False. Because hello world has space character which is not the alphabet.
isdigit method:
isdigit method is used to identify the string consists of only digits or not.
a=”1234″
The string consists of only digits.
String Data Type in Python
In this class, we discuss string data type in python.
String Data Type
The string is a sequence of Unicode characters. What’s Unicode we discuss in our following classes.
Let’s take an example and understand strings.
a=” Hello World.”
The above example variable a is having value Hello World.
Variable a is of string data type. Because it is having a sequence of characters.
The series of characters is given in double quotes. we can provide them in single quotes.
, is considered as a character. And space is a character.
We discuss all the characters in our coming classes.
We can use three quotes when the string is in multi-line. The example is shown below.
Memory Allocation to Strings
From the above diagram, we can observe.
Each character is assigned a memory space, and it is contiguous memory.
Total, we have eleven characters.So the index is 0,1,2,…10.
In python, it will give negative indexes. Shown in the above diagram.
Negative indexes start from the last character to the first. starting from -1,-2,-3…
Accessing Characters from the String
We use square brackets to access the characters from the string.
Example:
The above example displays the character present in the first position. Character e is present in position 1.
String Operations
String slicing:
Slicing means accessing a part of the string.
position.
Similar to the range function we discussed previously.
Examples of Negative Indexing.
The first print statement displays the character present in the -1 index position. So character d is shown.
The output displayed from starting position to -5. Hello is our output.
The last print statement will display characters from the -5 to -1 position. ie -5,-4,-3,-2 positions.
+ operator
we call + concatenation operator.
we use a space character string concatenated between strings. example d=a+” “+b.
star operator
Star operator is used to repeating the string.
Example:
Membership operators
The above example returns True. Because character h present in string a. So it returns true.
Not in works precisely opposite of in operator.
Format operator %
String Methods
Capitalize Method:
This method is used to place capital character at the starting of the string.
Example:
lower method:
The lower method is used to lower case the characters.
upper method:
The upper method is used to upper case the characters.
count method:
The count method is used to count the number of occurrences of a given string.
where the search should start.
From index position 3. the count method starts to look for character l.
The count method will search in the positions given 3,4,5,6. Last position not considered.
find method:
find method is used to identify at what position the required string occurred in a given string.
Let’s take an example for easy understanding.
It will find only the first occurrence. even l occurred three times.
Same as the count method, we can give start and end positions. That works similarly to the count method.
Find method will return the value -1. If the given string has not occurred.
len function:
The len function is used to identify the length of the string.
a=” hello world.”
Split method:
Example:
Whenever it finds the space character, it will split.
Whenever it encounters # it will split the string.
join method:
join method used to join a sequence of strings.
For now, we understand the method to join.
separator=”#”
variable l is having a list of strings.
A list of strings is joined using the join method by taking a separator #.
isalpha method:
isalpha method is used to check the given string consist of only alphabets.
Let’s take an example.
will display False. Because hello world has space character which is not the alphabet.
isdigit method:
isdigit method is used to identify the string consists of only digits or not.
a=”1234″
The string consists of only digits.
Комментарии