String Data Type in Python
In this class, we discuss string data type in python.
For Complete YouTube Video: Click Here
String Data Type
Before understanding string data type. The reader should have some basic knowledge of numeric data types. Click here.
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.
a=””” Hello
world”””
Memory Allocation to Strings
From the above diagram, we can observe.
Each character is assigned a memory space, and it is contiguous memory.
Each character is given an index value. The index values starting from zero.
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:
a=” hello world.”
print(a[1])
The above example displays the character present in the first position. Character e is present in position 1.
String Operations
String slicing:
Let’s take examples and understand string slicing.
Slicing means accessing a part of the string.
b=” hello world.”
print(b[2:5)])
Print statement 2 shows the starting index, and 5 shows the ending index.
The above print statement displays characters in 2,3,4 positions.
Point to remember: ending index position is not considered here. i.e., up to ending index position.
Similar to the range function we discussed previously.
The range function will generate numbers up to ending value. not ending value.
Examples of Negative Indexing.
a=” Hello World.”
print(a[-1])
print(a[-2:])
print(a[:-5])
print(a[-5:-1])
The first print statement displays the character present in the -1 index position. So character d is shown.
The example is shown in the second print statement. The ending position is not given. Default it will consider up to the last place.
The output displayed from -2 to the last position. ld is our output.
In the third print statement starting position is not given. Default considers the starting position.
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.
a=” hello”
b=” world”
c = a+b
d=a+” “+b
print(c)
print(d)
The concatenation operator is used to combine two strings.
To give space between two concatenated strings.
we use a space character string concatenated between strings. example d=a+” “+b.
star operator
Star operator is used to repeating the string.
Example:
a=” hello”
print(2*a) will display hellohello.
Membership operators
Let’s take an example and understand in and not in membership operators.
These membership operators are used to identify the required present in string or not.
a=” hello”
print(“h” in a)
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 %
The discussion about the format operator is made in the input-output class.
String Methods
Capitalize Method:
This method is used to place capital character at the starting of the string.
Example:
a=” hello world.”
print(a.capitalize()) will display Hello world.
lower method:
The lower method is used to lower case the characters.
a=” Hello World.”
print(a.lower()) will display hello world.
upper method:
The upper method is used to upper case the characters.
a=” Hello World.”
print(a.lower()) will display HELLO WORLD.
count method:
The count method is used to count the number of occurrences of a given string.
a=” hello world.”
print(a.count(“l”)) will display 3. because character l occurred three times in hello world.
print(a.count(“l”,3)) will show 2. the value three inside the count method gives the position where the search should start.
From index position 3. the count method starts to look for character l.
print(a.count(“l”,3,7)) will display 1. because in the method, starting and ending index positions are given.
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.
a=” hello world.”
print(a.find(“l”)) will display 2. because it will display the position where l occur in hello world.
It will find only the first occurrence. even l occurred three times.
print(a.find(“l”,3)) will display 3
print(a.find(“l”,3,7)) will display 3
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.”
print(len(a) will display 11.
Split method:
The split method is used to split the string based on a given character.
If nothing is given as input. It will consider space to split the string.
Example:
a=” hello world this is python program.”
print(a.split()) will display list[“hello”,”world”,”this”,”is”,”python”,”program”]
Whenever it finds the space character, it will split.
a=”hello#world#this#is#python#program”
print(a.split(“#”)) will display [“hello”,”world”,”this”,”is”,”python”,”program”]
Whenever it encounters # it will split the string.
print(a.split(“#”,1). will display list [“hello”,”world#this#is#python#program]
One in split method informs to split only one time.
print(a.split(“lo”)) will display [“hel”,”world#this#is#python#program”].
join method:
join method used to join a sequence of strings.
How to take the sequence of strings?
We use a list that is not yet discussed. We discuss in our next classes.
For now, we understand the method to join.
separator=”#”
l=[“hello”,”world”,”this”,”is”,”python”,”program”]
s=separator.join(l)
print(s) will display “hello#world#this#is#python#program”
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.
a=” hello world.”
print(a.isalpha()) will display False. Because hello world has space character which is not the alphabet.
b=” hello”
print(b.isalpha()) display True.
isdigit method:
isdigit method is used to identify the string consists of only digits or not.
a=”1234″
print(a.isdigit()) will return True.
The string consists of only digits.
isalnum method:
isalnum is used to identify the string consist of either alphabets or numeric or both.
Examples:
a=”1234+”
print(a.isalnum()) return False. because of +.
a=”12567″
print(a.isalnum()) will return True. because of only digits.
a=”heis”
print(a.isalnum()) will display True.
a=”he123″
print(a.isalnum()) will display True.