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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

6.Strings and Dictionaries

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 6.Strings and Dictionaries 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

Strings

1. String syntax

2、Strings are sequences

3、String methods


Strings

Python語言真正發揮作用的一個地方是字符串的操作。 本節將介紹Python的一些內置字符串方法和格式化操作。

這種字符串操作模式經常出現在數據科學工作中,并且在這種情況下是Python的一大閃光之處。

1. String syntax

在之前的課程中已經看過了很多字符串的例子,但回顧一下,Python中的字符串可以使用單引號或雙引號來定義。 它們在功能上是等價的。

【1】

x = 'Pluto is a planet' y = "Pluto is a planet" x == yTrue

如果您的字符串包含單引號字符(例如表示撇號),則使用雙引號會很方便。同樣,如果用單引號將其包裝起來,很容易創建一個包含雙引號的字符串:

【2】

print("Pluto's a planet!") print('My dog is named "Pluto"')Pluto's a planet! My dog is named "Pluto"

如果我們在單引號字符串中包含單引號字母,Python會報錯:

【3】

'Pluto's a planet!'File "<ipython-input-3-16d2b0784e8c>", line 1'Pluto's a planet!'^ SyntaxError: invalid syntax

我們可以通過反斜杠“轉義”單引號來解決這個問題。

[4]

'Pluto\'s a planet!' "Pluto's a planet!"

下面這張表格總結了反斜杠的重要使用例子:

What you type...What you getexampleprint(example)
\'''What\'s up?'What's up?
\"""That's \"cool\""That's "cool"
\\\"Look, a mountain: /\\"Look, a mountain: /\
\n?"1\n2 3"1
2 3

最后一行,\n表示新的一行。

【5】

hello = "hello\nworld" print(hello) hello world

此外,Python的字符串三重引用語法讓我們按字面意思包括換行符(即只需在鍵盤上點擊'Enter',而不是使用特殊的'\ n'序列)。 我們已經在文檔字符串中看到了這一點,但我們可以在任何想要定義字符串的地方使用它們。

【6】

triplequoted_hello = """hello world""" print(triplequoted_hello) triplequoted_hello == hello hello worldTrue

print()函數會自動添加換行符,除非我們指定關鍵字參數end的值而不是默認值'\ n':

【7】

print("hello") print("world") print("hello", end='') print("pluto", end='') hello world hellopluto

2、Strings are sequences

字符串可以看作是有序字母集合,我們可以按照對列表的操作對字符串進行操作:

【8】

# Indexing planet = 'Pluto' planet[0] 'P' # Slicing planet[-3:] 'uto'

【9】

# How long is this string? len(planet) 5 # Yes, we can even loop over them [char+'! ' for char in planet] ['P! ', 'l! ', 'u! ', 't! ', 'o! ']

與列表最大不同的是它們是不可以修改的。

[10]

planet[0] = 'B' # planet.append doesn't work either --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-12-65a7701f8ef6> in <module>() ----> 1 planet[0] = 'B'2 # planet.append doesn't work eitherTypeError: 'str' object does not support item assignment

3、String methods

像list一樣,str也有很多有趣的方法,這里有幾個小例子:

[11]

# ALL CAPS claim = "Pluto is a planet!" claim.upper() 'PLUTO IS A PLANET!'

[12]

# all lowercase claim.lower() 'pluto is a planet!'

[13]

# Searching for the first index of a substring claim.index('plan') 11

[14]

claim.startswith(planet) True

[15]

claim.endswith('dwarf planet') False

在字符串和列表之間:.split()和.join()

str.split()將字符串拆成更小的字符串,默認情況下以空格為界;當把字符串拆分成單詞列表時很有用。

[16]

words = claim.split() words ['Pluto', 'is', 'a', 'planet!']

有時你還想除了空格以外的東西:

[17]

datestr = '1956-01-31' year, month, day = datestr.split('-')

str.join()將我們帶到另一個方向,將一個字符串列表拼接成一個長字符串,使用它作為分隔符調用的字符串。

[18]

'/'.join([month, day, year]) '01/31/1956'

[19]

# Yes, we can put unicode characters right in our string literals :) '

總結

以上是生活随笔為你收集整理的6.Strings and Dictionaries的全部內容,希望文章能夠幫你解決所遇到的問題。

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