python基础 字符串 用str函数或repr函数将特殊字码转换为字字符串

版权声明:伊凡ED原创 https://blog.csdn.net/Ivan_ED/article/details/81973818

python基础 用str函数或repr函数将特殊字码转换为字符串


为避免错误 有时在编程内会遇到特殊字符 可调用str函数或者repr函数将非字符串表示为字符串
例如:

>>>xxx = 2012
>>>print('hello '+'world '+xxx) 
错误

#但如果利用str函数或repr函数
>>>xxx = 2012
>>>print('hello '+'world '+str(xxx))
>>>print('hello '+'world '+repr(xxx))
>>>hello world 2012

猜你喜欢

转载自blog.csdn.net/Ivan_ED/article/details/81973818
今日推荐