Python 连接字符串(join %)

yipeiwu_com6年前Python基础

join 方法用于连接字符串数组 


s = ['a', 'b', 'c', 'd'] 
print ''.join(s) 
print '-'.join(s)

输出结果:

abcd 
a-b-c-d


使用 % 连接多个变量


a = 'hello' 
b = 'python' 
c = 1 
print '%s %s %s %s' % (a, b, c, s)


输出结果:

hello python 1 ['a', 'b', 'c', 'd']


相关文章

pytorch 实现张量tensor,图片,CPU,GPU,数组等的转换

1, 创建pytorch 的Tensor张量: torch.rand((3,224,224)) #创建随机值的三维张量,大小为(3,224,224) torch.Tensor([...

linux下python抓屏实现方法

本文实例讲述了linux下python抓屏实现方法。分享给大家供大家参考。具体实现代码如下: #!/usr/bin/python '''by zevolo, 2012.12.20 '...

利用python3 的pygame模块实现塔防游戏

利用python3 的pygame模块实现塔防游戏

利用python3的pygame模块基本实现塔防游戏的基本功能,包括血量和分数显示,bgm,防御塔建造,防御塔攻击范围内的敌军,暂停和加速功能。由于实在没有素材,用的都是自己截图P的,所...

TensorFlow安装及jupyter notebook配置方法

tensorflow利用anaconda在ubuntu下安装方法及jupyter notebook运行目录及远程访问配置 Ubuntu下安装Anaconda bash ~/file_...

python list元素为tuple时的排序方法

如下所示: dist = [('m',5),('e',4),('c',9),('d',1)] dist.sort(key= operator.itemgetter(0)) print...