小猿圈python學(xué)習(xí)-細講數(shù)據(jù)類型-字符串

定義

字符串是一個有序的字符的集合央串,用于存儲和表示基本的文本信息蚂踊,' '或'' ''或''' '''中間包含的內(nèi)容稱之為字符串

創(chuàng)建:

s = 'Hello,Eva约谈!How are you?'

特性:

按照從左到右的順序定義字符集合,下標從0開始順序訪問犁钟,有序

可以進行切片操作

不可變棱诱,字符串是不可變的,不能像列表一樣修改其中某個元素涝动,所有對字符串的修改操作其實都是相當(dāng)于生成了一份新數(shù)據(jù)迈勋。

補充:

1.字符串的單引號和雙引號都無法取消特殊字符的含義,如果想讓引號內(nèi)所有字符均取消特殊意義醋粟,在引號前面加r靡菇,如name=r'l\thf'

字符串的常用操作

字符串操作方法有非常多,但有些不常用 米愿,我們只講重要的一些給大家厦凤,其它100年都用不上的有興趣可以自己研究

def capitalize(self):?

? ? 首字母大寫

def casefold(self):?

? ? 把字符串全變小寫

? ? >> > c = 'Alex Li'

? ? >> > c.casefold()

? ? 'alex li'

def center(self, width, fillchar=None):?

? ? >> > c.center(50, "-")

? ? '---------------------Alex Li----------------------'

def count(self, sub, start=None, end=None):?

? ? """

? ? S.count(sub[, start[, end]]) -> int

? ? >>> s = "welcome to apeland"

? ? >>> s.count('e')

? ? 3

? ? >>> s.count('e',3)

? ? 2

? ? >>> s.count('e',3,-1)

? ? 2

def encode(self, encoding='utf-8', errors='strict'):?

? ? """

? ? 編碼,日后講

def endswith(self, suffix, start=None, end=None):

? ? >> > s = "welcome to apeland"

? ? >> > s.endswith("land") 判斷以什么結(jié)尾

? ? True

def find(self, sub, start=None, end=None):?

? ? """

? ? S.find(sub[, start[, end]]) -> int

? ? Return the lowest index in S where substring sub is found,

? ? such that sub is contained within S[start:end].? Optional

? ? arguments start and end are interpreted as in slice notation.

? ? Return -1 on failure.

? ? """

? ? return 0

def format(self, *args, **kwargs):? # known special case of str.format

? ? >> > s = "Welcome {0} to Apeland,you are No.{1} user."

? ? >> > s.format("Eva", 9999)

? ? 'Welcome Eva to Apeland,you are No.9999 user.'

? ? >> > s1 = "Welcome {name} to Apeland,you are No.{user_num} user."

? ? >> > s1.format(name="Alex", user_num=999)

? ? 'Welcome Alex to Apeland,you are No.999 user.'

def format_map(self, mapping):?

? ? """

? ? S.format_map(mapping) -> str

? ? Return a formatted version of S, using substitutions from mapping.

? ? The substitutions are identified by braces ('{' and '}').

? ? """

? ? 講完dict再講這個

def index(self, sub, start=None, end=None):?

? ? """

? ? S.index(sub[, start[, end]]) -> int

? ? Return the lowest index in S where substring sub is found,

? ? such that sub is contained within S[start:end].? Optional

? ? arguments start and end are interpreted as in slice notation.

? ? Raises ValueError when the substring is not found.

? ? """

def isdigit(self):?

? ? """

? ? S.isdigit() -> bool

? ? Return True if all characters in S are digits

? ? and there is at least one character in S, False otherwise.

? ? """

? ? return False

def islower(self):?

? ? """

? ? S.islower() -> bool

? ? Return True if all cased characters in S are lowercase and there is

? ? at least one cased character in S, False otherwise.

? ? """

def isspace(self):?

? ? """

? ? S.isspace() -> bool

? ? Return True if all characters in S are whitespace

? ? and there is at least one character in S, False otherwise.

? ? """

def isupper(self):?

? ? """

? ? S.isupper() -> bool

? ? Return True if all cased characters in S are uppercase and there is

? ? at least one cased character in S, False otherwise.

? ? """

def join(self, iterable):?

? ? """

? ? S.join(iterable) -> str

? ? Return a string which is the concatenation of the strings in the

? ? iterable.? The separator between elements is S.

? ? """

? ? >>> n = ['alex','jack','rain']

? ? >>> '|'.join(n)

? ? 'alex|jack|rain'

def ljust(self, width, fillchar=None):?

? ? """

? ? S.ljust(width[, fillchar]) -> str

? ? Return S left-justified in a Unicode string of length width. Padding is

? ? done using the specified fill character (default is a space).

? ? """

? ? return ""

def lower(self):?

? ? """

? ? S.lower() -> str

? ? Return a copy of the string S converted to lowercase.

? ? """

? ? return ""

def lstrip(self, chars=None):?

? ? """

? ? S.lstrip([chars]) -> str

? ? Return a copy of the string S with leading whitespace removed.

? ? If chars is given and not None, remove characters in chars instead.

? ? """

? ? return ""

def replace(self, old, new, count=None):?

? ? """

? ? S.replace(old, new[, count]) -> str

? ? Return a copy of S with all occurrences of substring

? ? old replaced by new.? If the optional argument count is

? ? given, only the first count occurrences are replaced.

? ? """

? ? return ""

def rjust(self, width, fillchar=None):?

? ? """

? ? S.rjust(width[, fillchar]) -> str

? ? Return S right-justified in a string of length width. Padding is

? ? done using the specified fill character (default is a space).

? ? """

? ? return ""

def rsplit(self, sep=None, maxsplit=-1):?

? ? """

? ? S.rsplit(sep=None, maxsplit=-1) -> list of strings

? ? Return a list of the words in S, using sep as the

? ? delimiter string, starting at the end of the string and

? ? working to the front.? If maxsplit is given, at most maxsplit

? ? splits are done. If sep is not specified, any whitespace string

? ? is a separator.

? ? """

? ? return []

def rstrip(self, chars=None):?

? ? """

? ? S.rstrip([chars]) -> str

? ? Return a copy of the string S with trailing whitespace removed.

? ? If chars is given and not None, remove characters in chars instead.

? ? """

? ? return ""

def split(self, sep=None, maxsplit=-1):?

? ? """

? ? S.split(sep=None, maxsplit=-1) -> list of strings

? ? Return a list of the words in S, using sep as the

? ? delimiter string.? If maxsplit is given, at most maxsplit

? ? splits are done. If sep is not specified or is None, any

? ? whitespace string is a separator and empty strings are

? ? removed from the result.

? ? """

? ? return []

def startswith(self, prefix, start=None, end=None):?

? ? """

? ? S.startswith(prefix[, start[, end]]) -> bool

? ? Return True if S starts with the specified prefix, False otherwise.

? ? With optional start, test S beginning at that position.

? ? With optional end, stop comparing S at that position.

? ? prefix can also be a tuple of strings to try.

? ? """

? ? return False

def strip(self, chars=None):?

? ? """

? ? S.strip([chars]) -> str

? ? Return a copy of the string S with leading and trailing

? ? whitespace removed.

? ? If chars is given and not None, remove characters in chars instead.

? ? """

? ? return ""

def swapcase(self):?

? ? """

? ? S.swapcase() -> str

? ? Return a copy of S with uppercase characters converted to lowercase

? ? and vice versa.

? ? """

? ? return ""

def upper(self):?

? ? """

? ? S.upper() -> str

? ? Return a copy of S converted to uppercase.

? ? """

? ? return ""

def zfill(self, width):?

? ? """

? ? S.zfill(width) -> str

? ? Pad a numeric string S with zeros on the left, to fill a field

? ? of the specified width. The string S is never truncated.

? ? """

? ? return ""

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末育苟,一起剝皮案震驚了整個濱河市较鼓,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌违柏,老刑警劉巖博烂,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異漱竖,居然都是意外死亡脖母,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進店門闲孤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人烤礁,你說我怎么就攤上這事讼积。” “怎么了脚仔?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵勤众,是天一觀的道長。 經(jīng)常有香客問我鲤脏,道長们颜,這世上最難降的妖魔是什么吕朵? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮窥突,結(jié)果婚禮上努溃,老公的妹妹穿的比我還像新娘。我一直安慰自己阻问,他們只是感情好梧税,可當(dāng)我...
    茶點故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著称近,像睡著了一般第队。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上刨秆,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天凳谦,我揣著相機與錄音,去河邊找鬼衡未。 笑死尸执,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的眠屎。 我是一名探鬼主播剔交,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼改衩!你這毒婦竟也來了岖常?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤葫督,失蹤者是張志新(化名)和其女友劉穎竭鞍,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體橄镜,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡偎快,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了洽胶。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片晒夹。...
    茶點故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖姊氓,靈堂內(nèi)的尸體忽然破棺而出丐怯,到底是詐尸還是另有隱情,我是刑警寧澤翔横,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布读跷,位于F島的核電站,受9級特大地震影響禾唁,放射性物質(zhì)發(fā)生泄漏效览。R本人自食惡果不足惜无切,卻給世界環(huán)境...
    茶點故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望丐枉。 院中可真熱鬧哆键,春花似錦、人聲如沸矛洞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽沼本。三九已至噩峦,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間抽兆,已是汗流浹背识补。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留辫红,地道東北人凭涂。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像贴妻,于是被迫代替她去往敵國和親切油。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,332評論 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    網(wǎng)事_79a3閱讀 12,073評論 3 20
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,456評論 0 13
  • 大蜚山名惩,坐落于仙游澎胡,與發(fā)小會于山底,欲登高而望遠娩鹉。適逢見挫攻谁,余困于悲喜,擾于幸厄弯予。尋得閑暇戚宦,著此文以求勖之勉之⌒饽郏—...
    公子白洛閱讀 464評論 0 1
  • 昨天晚上成都終于下起了雨受楼,仿佛在房間都能聞到新鮮的空氣。最近咳嗽使我非常難受呼寸,就像我朋友圈說的“一周時間讓我咳出了...
    夜雨寒冬閱讀 225評論 0 0