current position:Home>Python from 0 to 1 (day 12) - Python data application 2 (STR function)
Python from 0 to 1 (day 12) - Python data application 2 (STR function)
2022-01-31 22:06:11 【My family】
This is my participation 11 The fourth of the yuegengwen challenge 17 God , Check out the activity details :2021 One last more challenge
Data conversion
There are functions responsible for converting data types 3 Kind of :str() 、 int() and float() .
below , Let's study first , Can help us solve the code just now Bug, Namely str() function , It can convert other types of data into str type .
str()
function
str()
Function to convert data to its string type , No matter what the data is int Type or float type , Just put it in brackets . This data can be changed , Become a string type .
below , Please see how it is used str()
Function to solve error reporting , Completion of data splicing .
who = ' I '
action = ' To be in '
destination = ' Meituan takeout '
how = ' spot '
number = 10
what = ' Fried chicken '
print(who + action + destination + how + str(number) + what)
Copy code
Isn't it quite simple ? We just need to go through str(number)
One step , Can will Integer types Of 【10】 Turn into String type Of 【10】, Data splicing completed successfully .
The results are printed successfully below 10 yes 2 and 5 Multiple .
magic = " Multiple "
action = " yes "
unit_and = " and "
uint = " Of "
number1 = 10
number2 = 2
number3 = 5
print(str(number1)+action+str(number2)+unit_and+str(number3)+unit+magic)
Copy code
Integers transformation String type Is the method very convenient ? Then if I tell you , There is also a way to convert to a string , And you've learned this method ?
print("10"+action+"2"+unit_and+"5"+unit+magic)
Copy code
below , Please run it , See if there can be two identical contents :
magic = " Multiple "
action = " yes "
unit_and = " and "
uint = " Of "
number1 = 10
number2 = 2
number3 = 5
print(str(number1)+action+str(number2)+unit_and+str(number3)+unit+magic)
print("10"+action+"2"+unit_and+"5"+unit+magic)
Copy code
You see , Do we write in two different ways :str()
Functions and quotes , The same result is output ?
But , Why? quotes We use numbers , Not variable names number Well ? This is because , When we use quotes when , Things in quotes , Will be cast to character string Format .
therefore , If we put the variable name number In the quotes Back inside , Is cast to a string , Will be the variable name number, Not the number represented by the variable name 10.
Maybe someone will make complaints about it in their hearts. : since quotes With so much trouble , Then I'll just use str() Chant ~ You see how easy it is .
Actually , I'm not just teaching you programming , But to convey to you a kind of programming thinking —— All roads lead to Rome .
The world of code is ever-changing , There may be millions of answers to the same result , If we just take it easy , Limited to one mode of thinking . Then this mindset will certainly hinder your progress , Stop you from being true · bosses .
therefore , I hope you can innovate , Try different options , Like my ancestors said : Calibrated boldness , Absolutely .
summary
str()
Function to convert data to its string type , No matter what the data is int Type or float type , Just put it in brackets . This data can be changed , Become a string type .
copyright notice
author[My family],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201312206104231.html
The sidebar is recommended
- leetcode 1560. Most Visited Sector in a Circular Track(python)
- leetcode 1995. Count Special Quadruplets(python)
- How to program based on interfaces using Python
- leetcode 1286. Iterator for Combination(python)
- leetcode 1418. Display Table of Food Orders in a Restaurant (python)
- Python Matplotlib drawing histogram
- Python development foundation summary (VII) database + FTP + character coding + source code security
- Python modular package management and import mechanism
- Django serialization (II)
- Python dataloader error "dataloader worker (PID XXX) is killed by signal" solution
guess what you like
-
apache2. 4 + Django + windows 10 Automated Deployment
-
leetcode 1222. Queens That Can Attack the King(python)
-
leetcode 1387. Sort Integers by The Power Value (python)
-
Tiger sniffing 24-hour praise device, a case with a crawler skill, python crawler lesson 7-9
-
Python object oriented programming 01: introduction classes and objects
-
Baidu Post: high definition Python
-
Python Matplotlib drawing contour map
-
Python crawler actual combat, requests module, python realizes IMDB movie top data visualization
-
Python classic: explain programming and development from simple to deep and step by step
-
Python implements URL availability monitoring and instant push
Random recommended
- Python avatar animation, come and generate your own animation avatar
- leetcode 1884. Egg Drop With 2 Eggs and N Floors(python)
- leetcode 1910. Remove All Occurrences of a Substring(python)
- Python and binary
- First acquaintance with Python class
- [Python data collection] scrapy book acquisition and coding analysis
- Python crawler from introduction to mastery (IV) extracting information from web pages
- Python crawler from entry to mastery (III) implementation of simple crawler
- The apscheduler module in Python implements scheduled tasks
- 1379. Find the same node in the cloned binary tree (Java / C + + / Python)
- Python connects redis, singleton and thread pool, and resolves problems encountered
- Python from 0 to 1 (day 11) - Python data application 1
- Python bisect module
- Python + OpenGL realizes real-time interactive writing on blocks with B-spline curves
- Use the properties of Python VTK implicit functions to select and cut data
- Learn these 10000 passages and become a humorous person in the IT workplace. Python crawler lessons 8-9
- leetcode 986. Interval List Intersections(python)
- leetcode 1860. Incremental Memory Leak(python)
- How to teach yourself Python? How long will it take?
- Python Matplotlib drawing pie chart
- Django paging (II)
- Concurrent. For Python concurrent programming Futures or multiprocessing?
- Programmers over the age of 25 can't know a few Chinese herbal medicines. Python crawler lessons 9-9
- Python crawler from introduction to pit full series of tutorials (detailed tutorial + various practical combat)
- The second bullet of class in Python
- Python object oriented programming 03: class inheritance and its derived terms
- How IOS developers learn Python Programming 13 - function 4
- Python crawler from introduction to mastery (VI) form and crawler login
- Python crawler from entry to mastery (V) challenges of dynamic web pages
- Deeply understand pandas to read excel, TXT, CSV files and other commands
- Daily python, Chapter 18, class
- "I just want to collect some plain photos in Python for machine learning," he said. "I believe you a ghost!"
- Django view
- Python implements filtering emoticons in text
- When winter comes, python chooses a coat with temperament for mom! Otherwise, there's really no way to start!
- Python crawler - get fund change information
- Highlight actor using Python VTK
- Python crawler actual combat: crawling southern weekend news articles
- leetcode 406. Queue Reconstruction by Height(python)
- leetcode 1043. Partition Array for Maximum Sum (python)