- 字符串截取
s = "hqello"
'''
print(s[0:3])
print(s[:])
print(s[::2])
'''
- 去空格(去左右空格)
print(s.strip())
print(s.lstrip())#去左空格
print(s.restrip())#去右空格
- 字符串复制
s_copy = s
print(id(s))
print(id(s_copy))
s = s.title()
s.title()
print(s_copy)
print(id(s))
print(id(s_copy))
s_copy= s[0:2]
print(id(s)
print(id(s_copy))
- 字符串拼接
s1 = "python"
#s2 = s + s1
#print(s2)
import operator
s2 = operator.concat(s,s1)
print(s2)
- 字符串比较
- lt(a,b)-----------小于
- le(a,b)-----------小于等于
- e(a,b)-----------小于等于
- eq(a,b)-----------等于
def eq(a,b):
return a==b
- ne(a,b)-----------不于
- ge(a,b)-----------大于等于
- gt(a,b)-----------大于
b = operator.lt(s,s1)
print(b)
print(s<s1)
5.字符串长度
print(len(s),len(s1))
字符串中最大最小字符
print(max(s))
print(min(s1))
- upper ------------转化为大写
- lower ------------转化为小写
- title ------------转化为标题(每个单词首字母大写)
- capitalize ------------首字母大写
- swapcase ------------大写变小写 小写变大写
s = "hqelloASd how are you"
print(s.upper())
print(s.lower())
print(s.title())
print(s.capitalize())
print(s.swapcase())
6.字符串分割
s = "hqelloASd how are you"
ss = s.split("o") #默认的话是空格拆
#->>>>>['hqell', 'ASd h', 'w are y', 'u']
print(ss)
7.字符串连接
s = "hqelloASd how are you"
s1 = "ghjk1"
s2 = s.join(s1)
print(s2)
a = ['hello','world']
str = "-"
print(str.join(a))
8.字符串内查找
s1 = 'today is a fine day'
index = s1.find("is",3,4)#3,4分别代表起始位置和终止位置
print(index)
9.字符串内替换
s1 = 'today is a fine day is'
s = s1.replace("is","are",2) #2代表替换前两个
print(s,s1)
10.字符串判断’
- isdigit -------------- 检测字符串中是否只由数字组成
- isalnum --------------检测字符串中是否只由数字和字母组成
- isalpha -------------- 检测字符串是否只由字母组成
- islower -------------- 检测字符串是否只含有小写字母
- isupper -------------- 检测字符串是否只含有大写字母
- isspace -------------- 检测字符串中是否只含有空格
- istitle -------------- 检测字符串中是否是标题(每个单词首字母大写)
s = " d "
print(s.isspace())
s =
Variables are used to store information to be referenced and manipulated in a computer program.
They also provide a way of labeling data with a descriptive name, so our programs can be understood
more clearly by the reader and ourselves. It is helpful to think of variables as containers that
hold information. Their sole purpose is to label and store data in memory. This data can then be
used throughout your program.
#取出所有to
c = s.find("to")
while c!=-1:
print(s[c:c+2])
c = s.find("to",c+2)
简单数据爬取
import urllib.request as ur
from lxml import etree
html_str = ur.urlopen("http://www.hxci.com.cn/index.php?m=content&c=index&a=lists&catid=193").read().decode("utf-8")
print(html_str)
html = etree.HTML(html_str)
print(html)
print(html.xpath('//h3/text()'))
up = ('tang', 'guo', 'li','xiu')
del up # 元素对象不支持删除