当前位置:网站首页>2.5 conversion of different data types (2)
2.5 conversion of different data types (2)
2022-07-03 20:00:00 【leeshuqing】
Continue to introduce the conversion of different data types . Let's take a look at the conversion between strings and integers .
First, let's look at the conversion between integer and single character . Let's start with a question , First of all, make it clear , The two cannot be mixed .
More common errors occur in the processing of input data .
num = input()
print(num + 1)
The operation interface is :
At this time, no matter what value is entered , Will produce the same error , It means that you can only connect strings with strings ( That is to add ).
Correct handling can be used int function :
num = input()
num = int(num)
print(num + 1)
The operation interface is :
in addition to , There is also a special conversion method , That is, convert a single character to the corresponding integer encoding .
strs = 'A'
print(ord(strs))
Output is :65. there ord The function returns the sequence number code corresponding to the character ,ord by ordinal( Serial number ) It means , Capital A The serial number code of is 65. Why? A Corresponding 65 Serial number ? This sequence number encoding is often referred to as a character set , Each common character corresponds to a unique sequence number . Different character sets may have different correspondence , But English characters are usually the same , Common character sets are ASCII、Unicode etc. .
This is a common character encoding :
Here for beginners , There is often a problem , Is the number 0 And integer 0 The difference between . Through the current character encoding , We can make an accurate distinction , In terms of integers , character 0 It's actually 48, Judging from the characters , Integers 0 It's actually a blank character .
Why should characters correspond to coding sequence numbers ? In the computer data storage structure , In fact, it can only store integers and floating-point numbers , Characters and Booleans cannot be stored directly . therefore , Computers usually express these types in a way of code mapping . For example, for characters , The computer can only store the serial number corresponding to these characters , This is an integer , If you need to convert to characters when reading , So the computer will look up the corresponding characters in the character coding table , And display the character .
The same is true for Booleans , What is really stored is 0 and 1 Two kinds of integers , Also through simpler encoding conversion ,0 Convert to False,1 Convert to True To realize the display of Boolean data .
Chinese characters also have coding numbers .
strs = ' south '
print(ord(strs))
Output is :21335.
In turn, , We can also use it chr Function converts an integer to a character , Is converted to the corresponding encoded characters .
num = 65
print(chr(num))
Output is :A.
Master these methods , We can use this integer encoding to skillfully convert characters . For example, change uppercase to lowercase :
strs = 'Z'
print(chr(ord(strs) + 32))
Output is :z. Here are lowercase letters “z”, The difference between the codes of upper and lower case letters is 32, Therefore, the case conversion can be realized by encoding the difference and accumulating the conversion .
Even if you don't know how much the case difference is , We can still finish :
strs = 'Z'
print(chr(ord(strs) + ord('a') - ord('A')))
The output content is the same as . You can experience the ingenious use of integers and characters for conversion .
Of course , It is simpler to use the existing lower case conversion function of string lower:
strs = 'Z'
print(strs.lower())
The output content is the same as .
For strings containing multiple numeric characters , You can also use int and str Functions directly convert to and from integers . Such as through int Function can directly convert a numeric string to the corresponding integer :
strs = '1234'
print(int(strs) + 1)
Output is :1235.
adopt str Function can directly convert an integer to the corresponding string :
num = 1234
print(str(num) + '1')
Output is :12341.
str It can also be used in other types of string conversion , Such as floating point numbers and Booleans :
num = True
print('[' + str(num) + ']')
Output is :[True].
because str The default is the name of the character conversion function , Therefore, it is suggested that when defining variables , Don't use str, Or in use str Function , Please try to avoid using str The definition form of the same name , Such as :
num = 1234
strs = str(num)
print(strs)
If you use str As a variable name, there may be problems , Such as :
num = 1234
str = 'ABCD'
print(str(num))
The operation interface is :
The error here is that the last line was intended to be converted num For the string , however str Functions and str Variable with the same name , In this time str Function failure , Of course, the last line can't depend on str String variable ‘ABCD’ To complete the real type conversion .
Speaking of this , We can understand the comparison of characters , In fact, it is the comparison of character encoded integers . such as :
str1 = 'A'
str2 = 'a'
print(str1 < str2)
Output is :True.
If it is a string with multiple characters , The comparison rule is to compare characters one by one from left to right , Once there is a character compared to the size , The comparison ends and takes this result as the size comparison result of the string , Be similar to 1 Yes 1 Penalty match :
such as :
str1 = 'aAaaa'
str2 = 'b'
print(str1 < str2)
Output is :True, Because the result of the first character comparison shows that the second string is larger .
But if two strings are compared , The front is consistent , There is another string and more characters , Then it is bigger :
str1 = 'aAaaa'
str2 = 'aAa'
print(str1 > str2)
Output is :True.
Supporting learning resources 、 MOOC video :
Python Big data analysis - Shu Qing Li https://www.njcie.com/python/
边栏推荐
- Global and Chinese market of full authority digital engine control (FADEC) 2022-2028: Research Report on technology, participants, trends, market size and share
- Chapter 2: 4-digit Kaplan number, search even digit Kaplan number, search n-digit 2-segment sum square number, m-digit ingenious square number without 0, specify the number to form a 7-digit square nu
- How to check the permission to write to a directory or file- How do you check for permissions to write to a directory or file?
- Microservice framework - frequently asked questions
- 第一章:喝汽水,阶梯电费计算,阶梯电费计算函数,个人所税,求解平方根不等式,简化求解平方根不等式,求解调和级数不等式,解不等式:d<1+1/2-1/3+1/4+1/5-1/6+..士1/n
- PR 2021 quick start tutorial, material import and management
- 44. Concurrent programming theory
- CMD implements the language conversion of locale non Unicode programs
- Day11 ---- 我的页面, 用户信息获取修改与频道接口
- 第一章:三位阶乘和数,图形点扫描
猜你喜欢
Leetcode 1189. Maximum number of balloons (special character count)
AcWing 1460. Where am i?
Acquisition and transmission of parameters in automatic testing of JMeter interface
Chapter 1: find the algebraic sum of odd factors, find the same decimal sum s (D, n), simplify the same code decimal sum s (D, n), expand the same code decimal sum s (D, n)
HCIA-USG Security Policy
第一章: 舍罕王失算
kubernetes集群搭建efk日志收集平台
04 -- QT OpenGL two sets of shaders draw two triangles
Detailed and not wordy. Share the win10 tutorial of computer reinstallation system
2022-06-27 advanced network engineering (XII) IS-IS overhead type, overhead calculation, LSP processing mechanism, route revocation, route penetration
随机推荐
02 -- QT OpenGL drawing triangle
Find a line in a file and remove it
第一章:求同吗小数和s(d, n)
BOC protected tryptophan zinc porphyrin (Zn · TAPP Trp BOC) / copper porphyrin (Cu · TAPP Trp BOC) / cobalt porphyrin (cobalt · TAPP Trp BOC) / iron porphyrin (Fe · TAPP Trp BOC) / Qiyue supply
10 smart contract developer tools that miss and lose
Global and Chinese markets of polyimide tubes for electronics 2022-2028: Research Report on technology, participants, trends, market size and share
CesiumJS 2022^ 源码解读[7] - 3DTiles 的请求、加载处理流程解析
Phpstudy set LAN access
Xctf attack and defense world crypto master advanced area olddriver
AcWing 1460. Where am i?
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
第一章:求所有阶乘和数,大奖赛现场统分程序设计,三位阶乘和数,图形点扫描,递归求n的阶乘n!,求n的阶乘n!,舍罕王失算
PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?
第一章: 舍罕王失算
Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
第一章:简化同码小数和s(d, n)
unittest框架基本使用
原生表格-滚动-合并功能
PR 2021 quick start tutorial, how to create a new sequence and set parameters?
Class loading process