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

歡迎訪問 生活随笔!

生活随笔

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

python

Learning python学习总结之字符串方法

發布時間:2023/12/31 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Learning python学习总结之字符串方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

總結下最近學習lerning python這本書的字符串部分的一些收獲吧。

一、原始字符串

在普通字符串前加‘r'即成為原始字符串,特點是抑制轉義,即在原始字符串中’\n‘這種轉義字符串沒有特殊含義了。

二、索引和分片

s = 'abcdefg'

s[1:5:2] = 'ace'

s[5:1:-1] = 'fedc'

s[::-1] = 'gfedcba'

三、字符串轉換工具

int('42') = 42

str(42) = '42'

ord('s') = 115

chr(115) = 's'

四、修改字符串

s = 'spam'

s[0] = 'a' ?error!!!不能原處修改

s = s + 'hello'

s = s[4:] + ' world' = 'hello world'

五、字符串方法

s = 'hello world'

(1) replace

s.replace( 'o', 'x' ) = 'hellx, wxrld'

(2) join

l = list( s ) = ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

' '.join( l ) = 'hello world'

','.join( ['hello', 'world'] ) = 'hello,world'

'spam'.join( ['hello', 'world'] ) = 'hellospamworld'

(3) split

line = 'aaa bbb ccc'

line.split() = ['aaa', 'bbb', 'ccc'] ? //如果不加任何參數默認用空格來分割

line = 'bob,hacker,40'

line.split( ',' ) = ['bob', 'hacker', '40']

(4) rstrip, lstrip ?//分別是去除字符串右端和左端的空白

line = 'the knights who say hi!\n'

line.rstrip() = 'the knights who sya hi!' ?//去除行末的空白

(5) endswith, startswith ?//rt

(6) find

'hello, world'.find( 'o' ) = 4


轉載于:https://blog.51cto.com/hychuanshuo/1239093

總結

以上是生活随笔為你收集整理的Learning python学习总结之字符串方法的全部內容,希望文章能夠幫你解決所遇到的問題。

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