python 数字类型和字符串类型的相互转换实例

yipeiwu_com5年前Python基础

一、python中字符串转换成数字

(方法1)

类中进行导入:import string

str='555'
num=string.atoi(str)

num即为str转换成的数字

转换为浮点数:string.atof(str)

(方法2)直接int

int(str)即可。

二、数字转换成字符串

num=322
str='%d'%num

str即为num转换成的字符串

以上这篇python 数字类型和字符串类型的相互转换实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python模拟登录之滑块验证码的破解(实例代码)

模拟登录之滑块验证码的破解,具体代码如下所示: # 图像处理标准库 from PIL import Image # web测试 from selenium import webdri...

pygame实现简易飞机大战

利用pygame实现了简易版飞机大战。源代码如下: # -*- coding:utf-8 -*- import pygame import sys from pygame.local...

详细介绍Python函数中的默认参数

import datetime as dt def log_time(message, time=None): if time is None: time=dt.da...

利用pyshp包给shapefile文件添加字段的实例

在已有的shapefile文件的基础上增加字段: # -*- coding:gb2312 -*- import shapefile r=shapefile.Reader(r"C:...

python里使用正则表达式的组嵌套实例详解

python里使用正则表达式的组嵌套实例详解 由于组本身是一个完整的正则表达式,所以可以将组嵌套在其他组中,以构建更复杂的表达式。下面的例子,就是进行组嵌套的例子: #python...