Data types help us classify data items. They determine the types of operations that we can perform on the data item. In azagar, joint quality Types of data Add SeatFor, for, for,. WireFor, for, for,. ListFor, for, for,. tupleFor, for, for,. BoveFor, for, for,. SetAnd Dictionary.
In this tutorial, we will focus on string data type. We will discuss how the string data type, string data type and the relationship between the ASCII table, the features of the string data, and how to announce some important wire methods and tasks.
What are the strings of Azigar?
A Wire There is an item that contains the alphabet. A character is a string of length. The same character is also a wire in Azar. Ironically, the character type of character data is absent in the programming language. However, we get the type of character data in other programming languages ​​such as C, Kotlin and Java.
We can announce the same quote, double quote, triple quote, or using it str()
The function shows a piece of code below how to announce the wire in Azar:
# A single quote string
single_quote = 'a' # This is an example of a character in other programming languages. It is a string in Python
# Another single quote string
another_single_quote = 'Programming teaches you patience.'
# A double quote string
double_quote = "aa"
# Another double-quote string
another_double_quote = "It is impossible until it is done!"
# A triple quote string
triple_quote = '''aaa'''
# Also a triple quote string
another_triple_quote = """Welcome to the Python programming language. Ready, 1, 2, 3, Go!"""
# Using the str() function
string_function = str(123.45) # str() converts float data type to string data type
# Another str() function
another_string_function = str(True) # str() converts a boolean data type to string data type
# An empty string
empty_string = ''
# Also an empty string
second_empty_string = ""
# We are not done yet
third_empty_string = """""" # This is also an empty string: ''''''
Is to use another way to get strings in azagar input()
Ceremony input()
The function allows us to enter values ​​in a program with the keyboard. The values ​​inserted are read as a wire, but we can convert them to other types of data.
# Inputs into a Python program
input_float = input() # Type in: 3.142
input_boolean = input() # Type in: True
# Convert inputs into other data types
convert_float = float(input_float) # converts the string data type to a float
convert_boolean = bool(input_boolean) # converts the string data type to a bool
We use type()
Function to determine an item data type in Azar. It returns the Object Class. When the Objects wire, he looted str
Class the same, it looted dict
For, for, for,. int
For, for, for,. float
For, for, for,. tuple
For, for, for,. bool
The class is when the object is a dictionary, number, float, tuple, or bold. Let’s use now type()
Function to determine the types of variables announced in previous code pieces:
# Data types/ classes with type()
print(type(single_quote))
print(type(another_triple_quote))
print(type(empty_string))
print(type(input_float))
print(type(input_boolean))
print(type(convert_float))
print(type(convert_boolean))
We have discussed how to announce the wires. Now let’s move towards the relationship between wires and the Ascii table.
ASCII Table vs. String Character
American Standard Code for Information Interchange (ascii) The letters or text were designed to help us make a map on numbers because the sets of numbers are easy to store in computer memory than texts. ASCII primarily encodes 128 characters in English language that are used to process information in computer and programming. English letters encode by ASCII include small characters (AZ), large letters (AZ), digits (0-9), and symbols such as symbols.
ord()
Function transforms a (one character) string of a (one character) of length to its decimal representation on the ASCII table, while chr()
The function turns the decimal representation into a wire. For example:
import string
# Convert uppercase characters to their ASCII decimal numbers
ascii_upper_case = string.ascii_uppercase # Output: ABCDEFGHIJKLMNOPQRSTUVWXYZ
for one_letter in ascii_upper_case(:5): # Loop through ABCDE
print(ord(one_letter))
65
66
67
68
69
# Convert digit characters to their ASCII decimal numbers
ascii_digits = string.digits # Output: 0123456789
for one_digit in ascii_digits(:5): # Loop through 01234
print(ord(one_digit))
48
49
50
51
52
In a piece of code mentioned above, we loop through the wires ABCDE
And 01234
And we turned each character into their decimal representation ASCII Table. We can also have reverse operation with the Lord chr()
The function, under which we convert the decimal numbers on the ASCII table into their string characters. For example:
decimal_rep_ascii = (37, 44, 63, 82, 100)
for one_decimal in decimal_rep_ascii:
print(chr(one_decimal))
%
,
?
R
d
On the ASCII table, the Strong characters in the output of the program map on their respective numbers. So far, we have discussed how to announce the wires and how the string character makes the map on the ASCII table. Next, let’s discuss the attributes of a wire.
Features of string
Zero Index: The first element in the wire contains a zero index, while the last factor index is len(string) - 1
. For example:
immutable_string = "Accountability"
print(len(immutable_string))
print(immutable_string.index('A'))
print(immutable_string.index('y'))
14
0
13
Instability. This means that we cannot update the roles in the stocking. For example, we cannot delete an element from the wire or try to assign a new element to any index position. If we try to update the string, it gives a throw TypeError
:
immutable_string = "Accountability"
# Assign a new element at index 0
immutable_string(0) = 'B'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11336/2351953155.py in
2
3 # Assign a new element at index 0
----> 4 immutable_string(0) = 'B'
TypeError: 'str' object does not support item assignment
However, we can re -assign a string immutable_string
Variable, but we should note that they are not the same string because they do not point to the same thing in memory. The old string does not update the object. It creates a new, as we can see by IDS:
immutable_string = "Accountability"
print(id(immutable_string))
immutable_string = "Bccountability"
print(id(immutable_string)
test_immutable = immutable_string
print(id(test_immutable))
2693751670576
2693751671024
2693751671024
You will have different identities for example because we are running our programs on different computers, so our memory leaves are different. However, both IDs should be different on the same computer. This means that both immutable_string
Various memory points to different addresses. We last assigned immutable_string
Variable test_immutable
Variable you can see it test_immutable
Variable and last immutable_string
The variable point of the same address.
Coincidence: Join two or more strings together to get a new string with +
As a symbol example:
first_string = "Data"
second_string = "quest"
third_string = "Data Science Path"
fourth_string = first_string + second_string
print(fourth_string)
fifth_string = fourth_string + " " + third_string
print(fifth_string)
Dataquest
Dataquest Data Science Path
Repetition: Can be repeated with a string *
As a symbol example:
print("Ha" * 3)
HaHaHa
Indicator and slices: We have already established that the indoor zero index is. We can access any element in string with its index value. We can also collide between two index values ​​and take a sub -section of a wire. For example:
main_string = "I learned R and Python on Dataquest. You can do it too!"
# Index 0
print(main_string(0))
# Index 1
print(main_string(1))
# Check if Index 1 is whitespace
print(main_string(1).isspace())
# Slicing 1
print(main_string(0:11))
# Slicing 2:
print(main_string(-18:))
# Slicing and concatenation
print(main_string(0:11) + ". " + main_string(-18:))
I
True
I learned R
You can do it too!
I learned R. You can do it too!
The ways of string
str.split(sep=None, maxsplit=-1):
There are two attributes in the string distribution method: sep
And maxsplit
. When this procedure is called with its default values, it separates its wire wherever there is a white space. This method returns a list of strings:
string = "Apple, Banana, Orange, Blueberry"
print(string.split())
('Apple,', 'Banana,', 'Orange,', 'Blueberry')
We can see that the wire is not well split because the splat -string contains ,
. We can use sep=','
To divide wherever one is ,
:
print(string.split(sep=','))
('Apple', ' Banana', ' Orange', ' Blueberry')
This is better than the previous division. However, we can see White Space before some parts of the distribution. We can take it off with it (sep=', ')
:
# Notice the whitespace after the comma
print(string.split(sep=', '))
('Apple', 'Banana', 'Orange', 'Blueberry')
Now, the wire is well divided. Sometimes, we don’t want to divide most of the time. We can use maxsplit
To explain that we intend to divide.
print(string.split(sep=', ', maxsplit=1))
print(string.split(sep=', ', maxsplit=2))
('Apple', 'Banana, Orange, Blueberry')
('Apple', 'Banana', 'Orange, Blueberry')
str.splitlines(keepends=False):
Sometimes we want to take action on different line -intermittent carps ('\n'
For, for, for,. \n\n'
For, for, for,. '\r'
For, for, for,. '\r\n'
) In the limits, we want to be divided into phrases, not individual words. We will use splitline
How to do this. While keepends=True
Line breaks are included in the text. Otherwise, they have been excluded. Let’s see how this is done with Shakespeare’s Macbobit text:
import nltk # You may have to pip install nltk
to use this library.
macbeth = nltk.corpus.gutenberg.raw('shakespeare-macbeth.txt')
print(macbeth.splitlines(keepends=True)(:5))
('(The Tragedie of Macbeth by William Shakespeare 1603)\n', '\n', '\n', 'Actus Primus. Scoena Prima.\n', '\n')
str.strip((chars)):
We remove the trailing white space or roles from both sides of the string with the Lord strip
Method for example:
string = " Apple Apple Apple no apple in the box apple apple "
stripped_string = string.strip()
print(stripped_string)
left_stripped_string = (
stripped_string
.lstrip('Apple')
.lstrip()
.lstrip('Apple')
.lstrip()
.lstrip('Apple')
.lstrip()
)
print(left_stripped_string)
capitalized_string = left_stripped_string.capitalize()
print(capitalized_string)
right_stripped_string = (
capitalized_string
.rstrip('apple')
.rstrip()
.rstrip('apple')
.rstrip()
)
print(right_stripped_string)
Apple Apple Apple no apple in the box apple apple
no apple in the box apple apple
No apple in the box apple apple
No apple in the box
In a piece of code mentioned above, we have used lstrip
And rstrip
The methods that remove white space or roles respectively from the left and right sides of the string. We have also used capitalize
The method, which transforms the wire into a phrase.
str.zfill(width):
zfill
Makes a string pad with the method 0
Ex -to receive the specified width
. For example:
example = "0.8" # len(example) is 3
example_zfill = example.zfill(5) # len(example_zfill) is 5
print(example_zfill)
000.8
str.isalpha():
This method returns True
If all the characters of the wire are alphabet. Otherwise, it looted False
:
# Alphabet string
alphabet_one = "Learning"
print(alphabet_one.isalpha())
# Contains whitspace
alphabet_two = "Learning Python"
print(alphabet_two.isalpha())
# Contains comma symbols
alphabet_three = "Learning,"
print(alphabet_three.isalpha())
True
False
False
Similarly, str.isalnum()
Recession True
If the characters of the wire are characters. str.isdecimal()
Recession True
If the wire characters are decorated. str.isdigit()
Recession True
If the wire characters are digits. And str.isnumeric()
Recession True
If the characters of the wire are numerical.
str.islower()
Recession True
If all the roles in the wire are small. str.isupper()
Recession True
If all the roles of the string are large, and str.istitle()
Recession True
If the first letter of each word is mass:
# islower() example
string_one = "Artificial Neural Network"
print(string_one.islower())
string_two = string_one.lower() # converts string to lowercase
print(string_two.islower())
# isupper() example
string_three = string_one.upper() # converts string to uppercase
print(string_three.isupper())
# istitle() example
print(string_one.istitle())
False
True
True
True
str.endswith(suffix)
Recession True
Does the wire end with a specific suffix? str.startswith(prefix)
Recession True
If the string begins with the specified former:
sentences = ('Time to master data science', 'I love statistical computing', 'Eat, sleep, code')
# endswith() example
for one_sentence in sentences:
print(one_sentence.endswith(('science', 'computing', 'Code')))
True
True
False
# startswith() example
for one_sentence in sentences:
print(one_sentence.startswith(('Time', 'I ', 'Ea')))
True
True
True
str.find(substring)
If the string is available, the lowest index returns. Otherwise, it returns -1. str.rfind(substring)
Most of the index returns. str.index(substring)
And str.rindex(substring)
If found, return the lowest and highest index of string respectively, respectively. If the string is not present in the string, they pick up ValueError
.
string = "programming"
# find() and rfind() examples
print(string.find('m'))
print(string.find('pro'))
print(string.rfind('m'))
print(string.rfind('game'))
# index() and rindex() examples
print(string.index('m'))
print(string.index('pro'))
print(string.rindex('m'))
print(string.rindex('game'))
6
0
7
-1
6
0
7
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11336/3954098241.py in
11 print(string.index('pro')) # Output: 0
12 print(string.rindex('m')) # Output: 7
---> 13 print(string.rindex('game')) # Output: ValueError: substring not found
ValueError: substring not found
str.maketrans(dict_map)
Produces a translation table from the map of the dictionary, and str.translate(maketrans)
The translation substitutes elements with its new values. For example:
example = "abcde"
mapped = {'a':'1', 'b':'2', 'c':'3', 'd':'4', 'e':'5'}
print(example.translate(example.maketrans(mapped)))
12345
String operations
Looping through a wire. Wires are not worth. Therefore, they support looping operations for loop
And enumerate
:
# For-loop example
word = "bank"
for letter in word:
print(letter)
b
a
n
k
# Enumerate example
for idx, value in enumerate(word):
print(idx, value)
0 b
1 a
2 n
3 k
String and relative operators: When two strings are compared using relevant operators (>
For, for, for,. <
For, for, for,. ==
Etc.), the elements of both strings are compared by their ASCII Number Index through the index. For example:
print('a' > 'b')
print('abc' > 'b')
False
False
In both cases, the production is False
. Relative operator first compared the number of ASCII -decimal number of elements on the index 0
For both wires. Since b
That’s more than a
It looted False
; Asciii -decimal, and in this case, the length of the wires does not matter.
When the strings are of the same length, it compares the ASCIII decimal -dimensional of each element from the index 0
Unless it is found elements with various ASCII -decimal numbers. For example:
print('abd' > 'abc')
True
In a piece of code mentioned above, the first two elements contain the same ascii -decimal number. However, the third element has a similarity, and since then d
That’s more than c
It looted True
. In a situation where all ASCII numbers are available for the elements, the long wire is more than the wire. For example:
print('abcd' > 'abc')
True
Checking a wire membership. in
The operator is used to check if there is a member of a string string:
print('data' in 'dataquest')
print('gram' in 'programming')
True
True
Another way to check a wire membership, replace a string, or the match sample is using Regular expression
import re
substring = 'gram'
string = 'programming'
replacement = '1234'
# Check membership
print(re.search(substring, string))
# Replace string
print(re.sub(substring, replacement, string))
pro1234ming
String formatting. f-string
And str.format()
Ways to form wires are used. Both use curly brackets {}
Place holders. For example:
monday, tuesday, wednesday = "Monday", "Tuesday", "Wednesday"
format_string_one = "{} {} {}".format(monday, tuesday, wednesday)
print(format_string_one)
format_string_two = "{2} {1} {0}".format(monday, tuesday, wednesday)
print(format_string_two)
format_string_three = "{one} {two} {three}".format(one=tuesday, two=wednesday, three=monday)
print(format_string_three)
format_string_four = f"{monday} {tuesday} {wednesday}"
print(format_string_four)
Monday Tuesday Wednesday
Wednesday Tuesday Monday
Tuesday Wednesday Monday
Monday Tuesday Wednesday
f-strings
Are more readable, and they are enforced faster than str.format()
The way, therefore, f-string
Strong formatting is a preferential way.
Handling quotation and apstrophy: apostrophy symbol (')
Represents a wire in Azar. To tell Azigar that we are not dealing with a wire, we have to use the character of the escape (\)
. So, an apostroper is represented \'
Unlike handling the appstroof in Azar, there are many ways to handle the quotation in Azar. These include the following:
# 1. Represent string with single quote (''
) and quoted statement with double quote (""
)
quotes_one = '"Friends don\'t let friends use minibatches larger than 32" - Yann LeCun'
print(quotes_one)
# 2. Represent string with double quote ("")
and quoted statement with escape and double quote (\"statement\")
quotes_two = "\"Friends don\'t let friends use minibatches larger than 32\" - Yann LeCun"
print(quotes_two)
# 3. Represent string (literally) with triple quote ("""""")
and quoted statment with double quote ("")
quote_three = """"Friends don't let friends use minibatches larger than 32" - Yann LeCun"""
print(quote_three)
"Friends don't let friends use minibatches larger than 32" - Yann LeCun
"Friends don't let friends use minibatches larger than 32" - Yann LeCun
"Friends don't let friends use minibatches larger than 32" - Yann LeCun
Conclusion
Azigar’s strings are incredible, and he is one of the basic types of data. We can announce them using single, double, or triple prices, or use str()
Ceremony
We can make each element of the wire a number on the ASCII table. This is the property we use when the strings are used using relative operators. There are many ways for processing strings.
This article discusses commonly used those, including ways to distribute wires, start and finish the characters, recruit, check the string case, and replace the elements in the wire. We have also discussed looping, string membership checks, formatting wires, and quotation marks and string operations to handle the quotation marks and appstropices through the string members.