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']


相关文章

介绍Python的@property装饰器的用法

在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9999 这显然不合逻...

ORM Django 终端打印 SQL 语句实现解析

ORM Django 终端打印 SQL 语句实现解析

在 settings.py 中添加以下内容: LOGGING = { 'version': 1, 'disable_existing_loggers': False, '...

Python代码块批量添加Tab缩进的方法

选择一个合适的编辑器,比如notepad++、VS、eclipse、sublime text等,选中要集体缩进的代码块, 按Tab:集体缩进(向右) 按Shift+Tab:集体回缩(向左...

python去掉字符串中重复字符的方法

复制代码 代码如下:If order does not matter, you can use "".join(set(foo))set() will create a set of u...

Python多继承顺序实例分析

本文实例讲述了Python多继承顺序。分享给大家供大家参考,具体如下: 示例1: #-*- coding:utf-8 -*- #!python2 class A(object):...