Python基础知识:字符串

1、Python中大写字母命名的变量,默认为常量,不可修改;列如:MYSQL_CONNECTION = '192.168.1.1'

2、字符串换行输入格式:换行用隔开,两行分别用引号,制表符 ,换行符

famous_person = "Albert Einstein"
message = famous_person+"once said,A person who never 
	"
        "made a mistake never tried anything new."
print(message)

3、type() 查看数据类型

#int整型
a=0
print(type(a))
#str字符串
b='charlie'
print(type(b))
#float浮点型
c=1.5
print(type(c))

4、布尔值:真或假  o==False   1==True

5、字符串基本操作

name = " char Lie,Alex "
print(' ' in name) #判断有没有空格
print(name.title())#首字母大写
print(name.upper())#全部变为大写
print(name.lower())#全部变为小写
print(name.rstrip())#删除末尾空白
print(name.lstrip())#删除开头空白
print(name.strip())#删除两端空白
print(name.replace(' ',''))#删除所有空白

6、split(),join(),字符串的分割与合并

name = 'alex,alice,james'
name2 = name.split(',')#字符串以逗号分割,变为列表
print(name2)
print('|'.join(name2))#将列表中的字符串以|合并为字符串

7、调用re模块,删除字符串中的特殊字符和空格

#删除特殊符号和空格
import re
s = 'charlie $ #   % * & daifu'
print(re.sub('[$#\%*& ]','',s))

8、格式化输出:%s-字符串,%d-整型,%f-浮点型

'''
字符格式化输出:
%s 输出字符串
%d 输出十进制整数
%f 输出浮点型数据(包括单双精度),以小数形式输出
'''
#2代表保留两位小数,四舍五入
s = "%.2f"%1.256
print(s)
#字符串中有%,需要两个%才能输出
s = "charlie %s %%3"%('is')
print(s)

#charlie is %3
#常用的字符串格式化功能
tpl = "i am %s" % "alex"
 
tpl = "i am %s age %d" % ("alex", 18)
 
tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
 
tpl = "percent %.2f" % 99.97623
 
tpl = "i am %(pp).2f" % {"pp": 123.425556, }
 
tpl = "i am %% %(pp).2f " % {"pp": 123.425556, }
#i am % 123.43
name=input("input your name:")
age=input("input your age:")
job=input("input your job:")
#多行字符串用两个三引号框起来
msg='''   
information of user %s:      
-------------------
name:    %s
age :    %s
job :    %s 
--------End--------
'''%(name,name,age,job)
print(msg)

9、format()字符格式化,变量存储在字典中

msg = 'Hello {name},it is {number} years no see.'
#使用**kwargs传递参数
msg1 = msg.format(name='alex',number=10)
print(msg1)
msg2 = msg.format(**{name='alex',number=10})
print(msg2)
#使用*args传递参数
msg = 'hello {0},{1} years no see.'
msg1 = msg.format('alex',10)
print(msg1)
msg2= msg.format(*['alex',10])
print(msg2)
#format()常用格式化
tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
  
tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
  
tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
  
tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
  
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
  
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
  
tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
  
tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
  
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
  
tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
  
tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
 
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
 
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
 
tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
 
tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)

10、center() 字符串在中间不满长度的用-填充

name = 'Lebran James'
print(name.center(40,'-'))

11、find() 检查字符串是否存在,如果存在返回索引,不存在则返回-1

name = 'Lebran James'
print(name.find('a'))
print(name.find('g'))

12、isalnum()检查字符串是否包含特殊字符(除了字母和数字,空格也算特殊)

       endswith('ab')检查以什么字符结尾

  startswith('Le')检查以什么字符开始

  isdigit()检查字符串是否为纯数字

name = 'Lebran James'
print(name.isalnum())#检查是否包含特殊字符(除了字母和数字,空格也算特殊)
print(name.endswith('es'))#检查以什么字符结尾
print(name.startswith('Le'))#以什么字符开始
number = '122'
print(number.isdigit())#检查字符串是否为纯数字

 13、capitalize()字符串的第一个字母大写

#字符串的第一个字母大写
name = 'lebran james'
name = name.capitalize()
print(name)
原文地址:https://www.cnblogs.com/charliedaifu/p/9928049.html