python字符串的增删改查

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python字符串的增删改查相关的知识,希望对你有一定的参考价值。

字符串增加

  1. 直接 +
name = ‘de8ug‘
city = ‘beijing‘
info = name + ‘ ‘ + city
info
‘de8ug beijing‘
  1. % 占位符替换
‘name: %s, place: %s‘%(name,city)                           
‘name: de8ug, place: beijing‘
  1. {}format 变量替换{} 或者 参数赋值
‘name:{},place:{}‘.format(name,city)                        
‘name:de8ug,place:beijing‘
‘name:{nameA},place:{placeA}‘.format(nameA=name,placeA=city)  
‘name:de8ug,place:beijing‘
  1. f-string
f‘name:{name},place:{city}‘
‘name:de8ug,place:beijing‘
  1. ‘ lilei said "where is han×××" ‘
    或者 " lilei said ‘where is han××ב " #双引号内加单引号或者单引号内加双引号

删除字符串

name = ‘de8ug‘
name = ‘‘
name
‘‘

字符串修改

name = ‘de8ug‘
name.upper()  #大写
‘DE8UG‘
name.lower()  #小写
‘de8ug‘
name.capitalize() #首字母大写
‘De8ug‘
name = "python is cool"
name.title()  #标题大写
‘Python Is Cool‘
name.strip(‘python‘) #去掉python(开头和结尾的空格或者字符串去掉)
‘ is cool‘
email = ‘de8ug#foxmail.com‘
email.replace(‘#‘,‘@‘)    #email.replace(old,new)
[email protected]
email.split(‘#‘)        #去除分隔符,变成列表
[‘de8ug‘, ‘foxmail.com‘]
email.zfill(30)
‘0000000000000de8ug#foxmail.com‘
email.ljust(30,‘8‘)   #email.ljust(width.fillchar)  左对齐再填充
‘de8ug#foxmail.com8888888888888‘
email.rjust(20,‘*‘)   #右对齐再填充
‘***de8ug#foxmail.com‘
email.center(20,‘*‘)   #中间对齐再填充
‘*de8ug#foxmail.com**‘

字符串查

1.字符串.方法()

‘de8ug‘.count(‘8‘)
1
name.isupper()  #判断是否为大写
False
name.islower()  #判断是否为小写
True
py = ‘python is cool‘
py.index(‘is‘)   #查询在哪个位置
7
py.index(‘o‘)
4
py.find(‘is‘)
7
py.find(‘isaaa‘)   #所有不存在为-1
-1
dir(py)  #看有哪些方法可用
[‘__add__‘,
 ‘__class__‘,
 ‘__contains__‘,
 ‘__delattr__‘,
 ‘__dir__‘,
 ‘__doc__‘,
 ‘__eq__‘,
 ‘__format__‘,
 ‘__ge__‘,
 ‘__getattribute__‘,
 ‘__getitem__‘,
 ‘__getnewargs__‘,
 ‘__gt__‘,
 ‘__hash__‘,
 ‘__init__‘,
 ‘__init_subclass__‘,
 ‘__iter__‘,
 ‘__le__‘,
 ‘__len__‘,
 ‘__lt__‘,
 ‘__mod__‘,
 ‘__mul__‘,
 ‘__ne__‘,
 ‘__new__‘,
 ‘__reduce__‘,
 ‘__reduce_ex__‘,
 ‘__repr__‘,
 ‘__rmod__‘,
 ‘__rmul__‘,
 ‘__setattr__‘,
 ‘__sizeof__‘,
 ‘__str__‘,
 ‘__subclasshook__‘,
 ‘capitalize‘,
 ‘casefold‘,
 ‘center‘,
 ‘count‘,
 ‘encode‘,
 ‘endswith‘,
 ‘expandtabs‘,
 ‘find‘,
 ‘format‘,
 ‘format_map‘,
 ‘index‘,
 ‘isalnum‘,
 ‘isalpha‘,
 ‘isdecimal‘,
 ‘isdigit‘,
 ‘isidentifier‘,
 ‘islower‘,
 ‘isnumeric‘,
 ‘isprintable‘,
 ‘isspace‘,
 ‘istitle‘,
 ‘isupper‘,
 ‘join‘,
 ‘ljust‘,
 ‘lower‘,
 ‘lstrip‘,
 ‘maketrans‘,
 ‘partition‘,
 ‘replace‘,
 ‘rfind‘,
 ‘rindex‘,
 ‘rjust‘,
 ‘rpartition‘,
 ‘rsplit‘,
 ‘rstrip‘,
 ‘split‘,
 ‘splitlines‘,
 ‘startswith‘,
 ‘strip‘,
 ‘swapcase‘,
 ‘title‘,
 ‘translate‘,
 ‘upper‘,
 ‘zfill‘]
my_name =input(‘name: ‘)
name: de8ug  
my_name
‘de8ug  ‘
len(my_name)   #可以包含空格
7
type(my_name)
str
my_name.strip()
‘de8ug‘
len(my_name.strip())
5
  1. 切片查询
py = ‘python2 python3 python4 python8‘
py[3]
‘h‘
py[-2]
‘n‘
py[:6]
‘python‘
py.index(‘2‘)
6
py[:py.index(‘2‘)]
‘python‘
something = ‘abcdefg1234567‘
something[1:6:2]   #索引1到6 步长为2
‘bdf‘
something[4:]  #左边包括
‘efg1234567‘
something[:4]  #右边不包括
‘abcd‘
something[4::4]
‘e26‘
something[something.index(‘e‘):]
‘efg1234567‘
email = ‘[email protected]
email.split(‘@‘)   #去除分隔符,变成列表
[‘de8ug‘, ‘foxmail.com‘]
email.index(‘@‘)
5
email[:email.index(‘@‘)]
‘de8ug‘
email.find(‘@‘)
5
email[email.find(‘@‘)+1:]   #左边包括
‘foxmail.com‘

以上是关于python字符串的增删改查的主要内容,如果未能解决你的问题,请参考以下文章

Python学习---django之ORM的增删改查180125

python之字典的增删改查

PHP怎么实现字符串的增删改查

Python_类和实例属性的增删改查操作

python-django框架-orm相关继续&ORM的增删改查操作_20191110

Python列表的增删改查