In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "string and digital example analysis of Python data type detailed explanation". In daily operation, I believe that many people have doubts about string and digital example analysis of Python data type. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "string and digital example analysis of Python data type". Next, please follow the editor to study!
Data type variable
Variables in Python do not need to be declared. Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.
In Python, a variable is a variable, it has no type, and what we mean by "type" is the type of object in memory that the variable refers to.
The equal sign (=) is used to assign values to variables.
To the left of the equal sign (=) operator is a variable name, and to the right of the equal sign (=) operator is the value stored in the variable.
Message = "hello,Python" print (message)
The running results are as follows:
There is an one-to-one correlation between variables and values, and when the program is running, a variable can only represent one value.
Python allows you to assign values to multiple variables at the same time. Such as:
A = b = c = 1a, b, c = 1,2, naming rules for "runoob" variables
Variable names can only contain letters, numbers, and underscores. You can start with a letter and an underscore, but not a number.
Variable names cannot contain spaces, but you can use underscores to split the words in them.
You cannot use the Python keyword as a variable name. Python's standard library provides a keyword module that outputs all keywords for the current version:
Import keywordprint (keyword.kwlist)
Note: use lowercase l and uppercase O with caution, as they may be mistaken for the numbers 1 and 0.
Variable names that generally use lowercase letters in Python. Although using uppercase letters in variable names does not cause errors, you should avoid using uppercase letters.
Standard data type
There are six standard data types in Python3:
Number (digital)
String (string)
List (list)
Tuple (tuple)
Set (collection)
Dictionary (dictionary)
Of the six standard data types of Python3:
Immutable data (3): Number (number), String (string), Tuple (tuple)
Variable data (3): List (list), Dictionary (dictionary), Set (collection).
String string (string) word = 'string' sentence = "this is a sentence." paragraph = "" this is a paragraph and can be made up of multiple lines "".
Single and double quotation marks are used exactly the same in python.
Use three quotation marks (''or "') to specify a multiline string.
Escape character\.
The backslash can be used to escape, and r can be used to prevent the backslash from escaping. If r "this is a line with\ n",\ nwill be displayed, not a line break.
Literally concatenating strings, such as "this", "is" and "string" are automatically converted to this is string.
Strings can be concatenated with the + operator and repeated with the * operator.
Strings in Python can be indexed in two ways, starting with 0 from left to right and-1 from right to left.
Strings in Python cannot be changed.
Python does not have a separate character type, and a character is a string of length 1.
The syntax format for intercepting a string is as follows: variable [header subscript: tail subscript: step size]
Str=' Python diary 'print (str) # output string print (str0:-1]) # output all characters print (str [0]) # output string first character print (str [2:5]) # output from the third to the fifth Character print (str2:) # output all characters from the third start print (str1: 5:2]) # output from the second to the fifth and every other character (step 2) print (str * 2) # output string twice print (str + 'Hello') # connection character The string print ('- -') print ('hello\ nrunoob') # uses a backslash (\) + n to escape the special character print (r'hello\ nrunoob') # and adds an r before the string Represents the original string and does not escape print ('\ n') # output blank line print (r'\ n') # output\ n >
The running results are as follows:
Escape character
Add a tab to the string, "\ t".
Add a newline character to the string, "\ n".
Backslash symbol, "\".
Single quotes, "'".
Double quotation marks, ".
Enter, "\ r".
Str1 = "Sleeping poem" str2 = "\ 'Stille\'\ t2021-12-13" str3 = "sleepy in spring, tired in autumn and summer nap\ nhibernation is not for a while" print (str1) print (str2) print (str3)
Modify string case
Change the first letter of each word to uppercase, title ().
Convert all letters to uppercase, upper ().
Convert all letters to lowercase, lower ().
Str = "hello,my dear Python world" print (str.title ()) print (str.upper ()) print (str.lower ())
Delete string whitespace
Remove the trailing white space, rstrip ().
Remove the opening white space, lstrip ().
Remove the beginning and end whitespace, strip ().
Str = "A" print (str) print (str.rstrip ()) print (str.lstrip ()) print (str.strip ())
If you can't see the difference between three or four, click at the end of the line, the third line of the cursor stays some distance after "A", and the fourth line stays after "A".
Judge that the string is full of letters or numbers
Judge that the string is full of letters, isalpha ().
Determine that the string is full of numbers, isdigit ().
Judge that the string has both letters and numbers, isalnum ().
Str1 = "abc" str2 = "123" str3 = "abc123" print ("whether all three strings are letters:") print (str1.isalpha ()) print (str2.isalpha ()) print (str3.isalpha ()) print ("whether all three strings are numbers:") print (str1.isdigit ()) print (str2.isdigit ()) print (str3.isdigit ()) print ("whether three strings contain both letters and numbers:") print () Str1.isalnum () print (str2.isalnum ()) print (str3.isalnum ())
String lookup
Search for the first word, startswith ().
The search of the last word, endswith ().
For words in any location, look up find () from left to right or rfind () from right to left.
Names = "Zhang San" print (names.startswith ("Zhang")) print (names.endswith ("four")) articles = "Love is not taking or possessing, but seeing you happy. Even if there is no longer me in this world, even if I can no longer protect you, I will become the wind and rain of the world, always by your side, forever …" Print (articles.find ("guardian") print (articles.rfind ("guardian"))
Among them, 38 is the position of the word "guardian", calculated from 0, no matter from left to right or from right to left, the position remains the same.
String substitution
Replace (), replace.
Articles = "Love is not taking or possessing, but seeing you happy. Even if there is no more me in this world, even if I can no longer protect you, I will become the wind and rain of the world, always by your side, forever." Print (articles.replace (Guardian, Protection))
Digital (Number)
Python3 supports int, float, bool, complex (plural).
In Python 3, there is only one integer type, int, represented as a long integer, and there is no Long in python2.
The built-in type () function can be used to query the object type referred to by the variable, and it can also be determined by isinstance, returning a Bool value.
A = 111print (isinstance (a, int))
The difference between isinstance and type is:
Type () does not consider a subclass to be a parent type.
Isinstance () will consider the subclass to be a parent type.
* * Note: * * in Python3, bool is a subclass of int. True and False can be added to numbers. True = = 1 and False = = 0 will return True, but the type can be determined by is.
Arithmetic operator
+ addition
-subtraction
* multiplication
/ division
% surplus
/ / integral division to fetch merchants
* * Power
Assignment operator
= assignment
+ = add value
-+ subtractive assignment
* = multiplicative assignment
/ = divide assignment
% = remainder assignment
/ / = integral division assignment
* = Power assignment
* * Note: * * in other languages, such as Candlespace journal java, there are self-increasing and subtracting operators "+" and "-", but they are positive or negative in Python.
At this point, the study on "Python data type detailed interpretation of string, numerical example analysis" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.