日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python字符串添加_python字符串的增删改查

發布時間:2023/12/18 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python字符串添加_python字符串的增删改查 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

字符串增加

直接 +

name = 'de8ug'

city = 'beijing'

info = name + ' ' + city

info

'de8ug beijing'

% 占位符替換

'name: %s, place: %s'%(name,city)

'name: de8ug, place: beijing'

{}format 變量替換{} 或者 參數賦值

'name:{},place:{}'.format(name,city)

'name:de8ug,place:beijing'

'name:{nameA},place:{placeA}'.format(nameA=name,placeA=city)

'name:de8ug,place:beijing'

f-string

f'name:{name},place:{city}'

'name:de8ug,place:beijing'

' 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)

'de8ug@foxmail.com'

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

切片查詢

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 = 'de8ug@foxmail.com'

email.split('@') #去除分隔符,變成列表

['de8ug', 'foxmail.com']

email.index('@')

5

email[:email.index('@')]

'de8ug'

email.find('@')

5

email[email.find('@')+1:] #左邊包括

'foxmail.com'

總結

以上是生活随笔為你收集整理的python字符串添加_python字符串的增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。