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

Random recommended