一.基本知识
1.双引号
字符串可以用双引号开始和结束。双引号中开始以双引号结束。spam="Test"
2.转义字符
双引号中需要有单引号,或者单引号中需要有双引号。
spam="T'B"'
打印出来是 T‘B
spam='T\'B'
打印出来是 T‘B
spam="A\"B"
打印出来是A"B
\'-------单引号转义字符
\"-----双引号转义字符
\t------制表符
\n-----换行符
\\-----倒斜杠
原始字符串
字符串开始打得引号之前加r.
spam=r"A’B"
4.三重引号
三重引号用于多行字符串,如:
spam=“asdasdasd
asdasd
asdasd”
此字符串由于Python的缩进机制会报错,改正:
spam=‘’‘’asdasdasd
asdasd
asdasd‘’‘
三重引号还用于注释。
5. 字符串下标和切片
spam='ABCDEF'
spam[0]----代表A
spam[start:stop[:step]---返回新的字符串
start---代表开始阶段
stop---字符串结束,倒数第一个为-1,倒数第二个为-2,以此类推。
step---步长,字符串开始截取第一个+步长+截取第二个(这个字符串的下标位置等于步长)
6.字符串的in和not in
source="ABCDEF"
print("ABC" in source)---返回True
print("ABC" not in source)--返回False
二.字符串常用方法
- 常用方法:
upper()-----字符串转化为大写,返回新的字符串,str,upper()
lower()----字符串转化为小写,返回新的字符串,str.lower()
isupper()—字符串中所有字符是否全部大写,返回BOOL
islower()—字符串中所有字符是否全部小写,返回BOOL - isX字符方法
isalpha()----如果字符串只包含**字母,**非空,返回BOOL
isalnum()----如果字符串只包含字母和数字,非空,返回BOOL
isdecimal()-----如果字符串只包含数字,非空,返回BOOL
isspace()—如果字符串只包空格、制符表、换行符,非空,返回BOOL
istitle()-如果字符串仅包含以大写字母开头,后面都是小写字母单词,返回BOOL - 字符串startwith(),endwith()
查看字符串开始或结束包含某个字符串,
str.startwith()----开始包含,返回BOOL(这里先按子串寻找)
str.endwith()–结束包含,返回BOOL(这里先按子串寻找) - 字符串方法join()和split()
如果有一个字符串列表,需要将他们连接起来,成为一个单独的字符串,join()方法可以调用,参数是一个字符串列表。
print(" ".join(['A','B'])) 返回新的字符串
--》A B
split() 分割字符串,返回一个字符串列表
- rjust()、ljust()、center()方法对其文本
rjust()和ljust()方法通过插入空格来对其文本。
print("A".rjust(20))#左补齐20个字符串,返回新字符串
>>> A
print("A".ljust(20))#右补齐20个字符串,返回新字符串
>>>A
print("A".center(20,"-"))#居中,以“-”字符来补齐,返回新字符串
>>>---------A----------
- strip() rstrip() Istrip()删除空白字符串
这个三个字符串都返回新的字符串
rstrip–删除右边空白字符串
Istrip–删除左边空白字符串
strip—删除两边字符串,有可选参数
print("ABCDFGHJJABDS".strip("ABC"))
>>>DFGHJJABDS
- pyperclip模块拷贝粘贴字符串
pyperclip中有copy() 和paste()函数,向计算机剪切板发送或接受文本
import pyperclip
print(pyperclip.copy("Asdasd"))#返回NONE,不一定要用这个函数,主要复制的都可以
print(pyperclip.paste())#返回剪切的内容
- 书上练习的脚本
#-*- coding:utf-8 -*-
'''
字符串练习项目需要实现的功能:
1.用户在本地的文件中保存了用户名和密码。
2.需要通过查询用户得到密码,再复制到剪切板上去,方便用户不用每次上不同网站输入密码。
3.同目录下建立一个文件来通过dict的格式储存不同的用户名和密码
4.{"user1":"123456","user2":"78946","user3":"ACBD"}
'''
import os
import pyperclip
import ast
class get_psd(object):
"""docstring for ClassName"""
def __init__(self):
pass
def search_pad(self,accout):
f=open("account_psd.txt","r")
account_info=f.readline()
f.close()
for i in ast.literal_eval(account_info):
if i==accout:
pyperclip.copy( ast.literal_eval(account_info)[i])#复制到剪切板
return ast.literal_eval(account_info)[i]
else:
return "未寻找此账号"
if __name__ == '__main__':
print(get_psd().search_pad("user1"))
'''
字符串转字典的三种方法:
1.json.loads(account_info) 数组或对象之中的字符串必须使用双引号,不能使用单引号
2. eval(account_info) 安全性不足
3.
import ast
ast.literal_eval(account_info)要用这个
'''
#-*- coding:utf-8 -*-
'''
实现功能如下:
1.从剪切板获取到内容后,处理后为一行一行的并且前面有*
如:
复制内容为:
123
4567
asdsd
处理后:
*123
*4567
*asdsd
'''
import pyperclip
import os
def paste():
txt=pyperclip.paste()
str_= txt.split("\n")#先处理换行
for i in range(len(str_)):
str_[i]="*"+str_[i]
print("".join(str_))
paste()
#-*- coding:utf-8 -*-
'''
编写一个函数,它接受字符串的列表的列表,将它显示在组织好的表格中,每列右对齐,嘉定所有的内层列表都包含同样数目的字符串。
分析:
获取到每个内层列表中的最大字符串长度,
再有三个列表中最大字符串长度进行右对齐。
table_data = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose'],
]
使用右对齐
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
# '''
table_data = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose'],
]
def print_Table(str_list):
col=[0]*len(str_list)
lengh=[]
list_y=len(str_list[0])
for x in range(len(str_list)):
for y in range(list_y):
lengh.append(len(str_list[x][y]))
col[x]=max(lengh)#每个列表中的长度
intWidth = max(col) #获取最大长度,右对齐的需要左边补
for x in range(list_y):
for y in range(len(str_list)):
print(str(str_list[y][x]).rjust(intWidth,"*"), end = '')#进行打印排列,这里end="",如果不设置的话会自动换行
print('\n')
#第一个打印后进行补,第二个在第一个基础上进行补字符,以此类推。
#
print_Table(table_data)