python中类型转化

s="123"
w=int(s)
print(type(s),type(w))

#输出结果

<class 'str'> <class 'int'>

当字符串里还包含非数字的时候:

s="123ab"
w=int(s)
print(w)
print(type(s),type(w))

#输出结果

C:\Users\yang\AppData\Local\Programs\Python\Python35\python.exe E:/pychar/project/pro/chen.py
Traceback (most recent call last):
  File "E:/pychar/project/pro/chen.py", line 5, in <module>
    w=int(s)
ValueError: invalid literal for int() with base 10: '123ab'

整形转换为字符串:

a=123456;
s=str(a)
print(type(a),type(s))

#输出结果
<class 'int'> <class 'str'>

 

posted @ 2017-10-20 14:45  傻逼学python  阅读(232)  评论(0编辑  收藏  举报