代码name = ‘中国人‘
num = 3print(‘我是‘ + name + ",今年有" + str(num) + "个愿望")
print(‘我是%s,今年有%d个愿望‘%(name, num))
print(‘我是{name},今年有{num}个愿望‘.format(num=num,name=name))
print(‘我是{},今年有{}个愿望‘.format(name, num))
print(‘我是‘, name, ‘,今年有‘, num, ‘个愿望‘, sep = ‘‘)
print(f‘我是{name},今年有{num}个愿望‘)
执行结果我是中国人,今年有3个愿望
我是中国...
Python2.6 开始,新增了一种格式化字符串的函数 str.format()。使用起来简单方便,不会遇到使用%时候格式的选择问题。按照参数默认顺序>>> "yesday is {}, today is {}".format("saturday", "sunday")
‘yesday is saturday, today is sunday‘
>>>指定参数顺序>>> "yesday is {0}, today is {1}, good day is {0}".format("saturday", "sunday")
‘yesday is saturday, today is sunday, good day is saturday‘
>>>指定参数名称#...
Python 字符串,列表,字典,集合的常用方法String 字符串常用方法# find 方法 查询字符串,返回子串所在的位置的最左段的索引# a = ‘asdfghjkl‘
# print(‘find 方法‘)
# print(a.find(‘asd‘))
# print(a.find(‘dfg‘, 5, 10))
# print(‘-‘*50)# join 方法是非常重要的字符串方法, 属于split方法的逆方法。用来连接序列中的元素,元素皆为字符串# b = [‘1‘, ‘2‘, ‘3‘, ‘4‘]
# print(‘join 方法‘)
# print(‘...
>>>‘abc‘ + ‘def‘‘abcdef‘Python分别为‘abc‘和‘def‘分配了空间,当进行连接操作时,Python自动为新的字符串‘abcdef‘分配了空间 >>>s = ‘abc‘>>>s = s + ‘def‘>>>s‘abcdef‘ 上面的例子中,看上去我们把‘abc‘赋给了s,然后在s的末尾添加了‘def‘。这样看起来字符串似乎是可变的其实不然,在"s +‘def‘"这个操作的时候,新建了一个新字符串,然后这个新的对象被赋给了s,原来的字符串‘abc‘被释放掉了我们可以...
字符串基础Python 也提供了可以通过几种不同方式表示的字符串。它们可以用单引号 (‘...‘) 或双引号 ("...") 标识 。\ 可以用来转义引号: >>> ‘spam eggs‘# single quotes‘spam eggs‘
>>> ‘doesn\‘t‘# use \‘ to escape the single quote..."doesn‘t"
>>> "doesn‘t"# ...or use double quotes instead"doesn‘t"
>>> ‘"Yes," he said.‘‘"Yes," he said.‘
>>> "\"Yes,\" he said."‘"Yes," he said.‘
>>> ‘"Isn\‘...
格式:\033[显示方式;前景色;背景色m说明:
前景色 背景色 颜色
---------------------------------------
30 40 黑色
31 41 红色
32 42 绿色
33 43 黄色
34 44 蓝色
35 45 紫红色
36 46 青蓝色
37...
Python学习系列(三)(字符串) 一个月没有更新博客了,最近工作上有点小忙,实在是没有坚持住,丢久又有感觉写的必要了,可见本人的坚持精神不佳,本系列没有任何目的,纯属业余学习,或者说是一时兴趣所致。通过本文,能够学习字符串的基本操作,日积月累,多多练习,学到了,会用了才是王道。一、基本概念1,关于转义问题1)“‘‘”方式:>>> s="Hello ‘Jack‘……"
>>> print s
Hello ‘Jack‘……2)\转义字符:>>> ...
#6个基本方法,要记住的join
split
find
strip
upper
lower#索引,下标:通过索引获取字符串中的某一个字符
test = ‘alex‘
v1 = test[0]
v2 = test[1]
v3 = test[2]
v4 = test[3]
v5 = test[0:1] #索引范围, 大于等于0,小于1
v6 = test[0:-1]
print(v1)
print(v2)
print(v3)
print(v4)
print(v5)
print(v6)# len() 字符串里面有多少个字符组成
v7 = len(test)
print(v7)# len() 对于列表的使用,计算列表中有多少个元素
li = [11,...
import datetime,time
stringDate = "2006-05-18 19:35:00"
dt = datetime.datetime.fromtimestamp(time.mktime(time.strptime(stringDate,"%Y-%m-%d %H:%M:%S")))
print dt
2006-05-18 19:35:00
>>> print type(dt)
<type ‘datetime.datetime‘>
>>>
http://www.sharejs.com/codes/python/835原文:http://www.cnblogs.com/linbinqiang/p/4919323.html
[本文出自天外归云的博客园]1、递归遍历目录下所有文件并通过finder函数定位指定格式字符串2、用来查找字符串的finder函数是自己定义的,这里定义了一个ip_port_finder通过正则表达式查找ip:port格式(粗匹配:数字.数字.数字.数字:数字)的字符串3、用gevent来实现协程并发完成耗时任务代码如下:# -*- coding: utf-8 -*-import re
from os.path import join
from os import walk
from gevent import monkey
import geventmonkey....
>>> str1="welcom to China">>> str1[2:4]‘lc‘>>> str1[7]‘t‘>>> str1=str1[:8]+"中国"+str1[8:]>>> str1‘welcom t中国o China‘>>> #本质上是重新创建了一个字符串并贴上str1的标签,原来被str1标签贴的字符串仍存在只是现在未被任何标签贴上,之后会被python自动回收机制收回。>>> str2=‘china‘>>> str2.capitalize()‘China‘>>> str2=‘CHINA is beautiful, HAHA‘>>> str2.casefold()‘china is beautiful, haha‘>>> ...
方法一:# -*- coding: utf-8 -*-# 利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法:def trim(s):while s[:1] == ‘ ‘:s = s[1:]while s[-1:] == ‘ ‘:s = s[0:-1]return s# 测试:
if trim(‘hello ‘) != ‘hello‘:print(‘测试失败!‘)
elif trim(‘ hello‘) != ‘hello‘:print(‘测试失败!‘)
elif trim(‘ hello ‘) != ‘hello‘:print(‘测试失败!‘)
elif trim(‘ hello...
笔记:# value = "raitOrEi"
# v = value.capitalize()#首字母大写
# print(v)
# v1 = v.casefold()#全部变小写,不只是英文的,其他语言特殊的大小写也变换
# print(v1)
# v2 = v.lower()#只是英文变小写
# print(v2)# 设置宽度,并将内容居中
# 20 代指总长度
# 只能填充一个字符,字符可有可无,没有字符用空格填充
# value = "raitorei"
# v = value.center(20)
# print(v)
# v1 = value.center(20,"*")
# print(v1)
# v2 = val...
strip方法用于去除字符串首尾空格例子:a = " abc "
a.strip() # abc 原文:https://www.cnblogs.com/luguankun/p/11901948.html
python 字符串的截取0 是第一个,-1是最后一个,只要是 负的,都是倒序的str = ‘welcome to China‘
a = str[3]
b = str[11:]
c = str[-1]
d = str[3:11]
f = str[0:]
print("a 输出的是:" + a)
print("b 输出的是:" + b)
print("c 输出的是:" + c)
print("d 输出的是:" + d)
print("f 输出的是:" + f)A = "ABCDEFGHIGK"
e1 = A[0:5]
e2 = A[0:5:2]
print("e1 输出的是:" + e1)
print("e2 输出的是:" + e2)print(A[6::-2])...