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

歡迎訪問 生活随笔!

生活随笔

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

python

python 字符ab+字符c 2_python入门 之 字符串(二)

發布時間:2023/12/15 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 字符ab+字符c 2_python入门 之 字符串(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

注:

來自:《Python 編程 從入門到實踐》

環境: windows Python 2.7

Python 之禪

1. 輸入代碼:

import this

2. 它從本質上闡述了代碼的指導原則,其內容如下:

'''The Zen of Python, by Tim Peters

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren't special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one-- and preferably only one --obvious way to do it.

Although that way may not be obvious at first unless you're Dutch.

Now is better than never.

Although never is often better than *right* now.

If the implementation is hard to explain, it's a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea -- let's do more of those!'''

'''優美勝于丑陋(以編寫優美的代碼為目標)

明了勝于晦澀(優美的代碼應當是明了的,命名規范,風格相似)

簡潔勝于復雜(優美的代碼應當是簡潔的,不要有復雜的內部實現)

復雜勝于凌亂(如果復雜不可避免,那代碼間也不能有難懂的關系)

扁平勝于嵌套(優美的代碼應當是扁平的,不能有太多的嵌套)

間隔勝于緊湊(優美的代碼有適當的間隔,不要奢望一行代碼解決問題)

重視可讀性(優美的代碼是可讀的)

盡管實用性會打敗純粹性,特例也不應該凌駕于規則之上

不要包容所有錯誤,除非你確定需要這樣做

面對不明確的定義,拒絕猜測的誘惑

找到一種最好唯一的一種方法去解決問題(如果不確定,就用窮舉法)

雖然這并不容易,因為你不是 Python 之父

做也許好過不做,但沒有思考過的做還不如不做

如果你無法向人描述你的方案,那肯定不是一個好方案

命名空間是一種絕妙的理念,請多加利用'''

3. 我們可以在Python27\Lib\this.py中看到該原則的實現:

#this.py

s = """Gur Mra bs Clguba, ol Gvz Crgref

Ornhgvshy vf orggre guna htyl.

Rkcyvpvg vf orggre guna vzcyvpvg.

Fvzcyr vf orggre guna pbzcyrk.

Pbzcyrk vf orggre guna pbzcyvpngrq.

Syng vf orggre guna arfgrq.

Fcnefr vf orggre guna qrafr.

Ernqnovyvgl pbhagf.

Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.

Nygubhtu cenpgvpnyvgl orngf chevgl.

Reebef fubhyq arire cnff fvyragyl.

Hayrff rkcyvpvgyl fvyraprq.

Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.

Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.

Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.

Abj vf orggre guna arire.

Nygubhtu arire vf bsgra orggre guna *evtug* abj.

Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.

Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.

Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!"""

'''chr: 用于將0~255范圍內的整數轉換為字符,比如:chr(65)為'A' chr(97)為'a''''d={}for c in (65, 97):for i in range(26):

d[chr(i+c)] = chr((i+13) % 26 +c)'''d = {'A': 'N', 'C': 'P', 'B': 'O', 'E': 'R', 'D': 'Q', 'G': 'T', 'F': 'S', 'I': 'V', 'H': 'U', 'K': 'X',

'J': 'W', 'M': 'Z', 'L': 'Y', 'O': 'B', 'N': 'A', 'Q': 'D', 'P': 'C', 'S': 'F', 'R': 'E', 'U': 'H',

'T': 'G', 'W': 'J', 'V': 'I', 'Y': 'L', 'X': 'K', 'Z': 'M', 'a': 'n', 'c': 'p', 'b': 'o', 'e': 'r',

'd': 'q', 'g': 't', 'f': 's', 'i': 'v', 'h': 'u', 'k': 'x', 'j': 'w', 'm': 'z', 'l': 'y', 'o': 'b',

'n': 'a', 'q': 'd', 'p': 'c', 's': 'f', 'r': 'e', 'u': 'h', 't': 'g', 'w': 'j', 'v': 'i', 'y': 'l',

'x': 'k', 'z': 'm'}'''

print "".join([d.get(c, c) for c in s])

對照著d的數據我們可以發現其關系為

大寫字母:A -> N,C -> P, B -> O ...

小寫字母: a -> n,? c -> p b -> o ...

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm

其對照圖為:

Gur Mra bs Clguba?參考對照圖就會變成 The Zen of Python。

這個屬于ROT13(回轉13位)的一種替換式密碼,屬于對稱加密。加密和解密都是同一套算法,屬于凱撒加密的一種變體,一段文字按照字母順序只需要將當前字母用13位之后的對應字母替代,超過第13位的字母則重新回到字母開頭。而該加密算法的最終實現就是python 設計之禪。

基本使用:

一般使用字符串通過單引號或者雙引號或者三引號標記,比如:

print('string')print("string")#三引號一般用于段落

_str = """ONe

Two

Three"""

print(_str)

假設說,我們想在字符串中使用單引號,比如:

#This is 'Python'

#單引號進行套用會報錯,我們可以使用雙引號:

print("This is 'Python'")#使用轉義字符

'''\' 代表單引號

\" 代表雙引號

\t 代表制表符

\n 代表換行符

\\ 代表倒斜杠'''

print('This is \'Python\'')print("This is \'Python\'")

在上面提到我們可以使用轉義字符實現一些特殊操作,如果想在程序中將轉義字符作為字符串打印出來的話,我們可以這樣做:

#在字符的前面添加 r 標記,說明這是原始字符串

print(r'This is \'Python\'') #This is \'Python\'

print('Hello\tPython') #Hello Python

print(r'Hello\tPython') #Hello\tPython

原始字符串在正則表達式中很有用,故此說明下。

其常用的運算符有:

運算符

描述

示例

+

字符串連接

'A' + 'b' --> Ab

*

重復輸出

'a' * 3 --> aaa

[]

根據索引獲取指定字符串

注意:不可超過字符最大長度

_str = 'A B'

_str[-1] --> B

_str[len(_str) -1] --> B

[:]

截取字符串指定部分

_str = 'ABC'

_str[0:2] --> AB

in

若包含指定字符,返回True

注意:區分大小寫

print('c' in 'AB') --> False

print('C' in 'ABC') --> True

not in

若不包含指定字符,返回False

注意:區分大小寫

print('c' not in 'AB') --> True

print('C' not in 'ABC') --> False

常用方法:

1. 數字與字符串的相互轉化:

print('Add:' + str(100)) #Add:100

print(int('100') * 2) #200

print(float('10') * 2) #20.0

2. 字符串的字母大小寫

#將字符串的首字母大寫

print('hi,boy'.title()) #Hi,Boy#將字符串所有字符大寫

print('hi,boy'.upper()) #HI,BOY#將字符串所有字符小寫

print('Hello World'.lower()) #hello world

3. 刪除字符串開頭或結尾空格

#刪除字符串開頭空格,若未重新賦值,其刪除是暫時的

strName = 'String'

print(strName.lstrip()) #String

print(strName) #String

#刪除字符串結尾空格,若未重新賦值,其刪除是暫時的

strName = 'String'

print(strName.rstrip() + 'Space') #StringSpace

print(strName + 'Space') #String Space

#刪除字符串首尾空格,若未重新賦值,其刪除是暫時的

strName = 'String'

print(strName.strip() + 'space') #StringSpace

print(strName + 'space') #String space

4. 獲取字符串長度及制定字符相關

#獲取字符串長度

print(len('He llo')) #6

#獲取字符串最大(最小)字符

print(max('abdef')) #f

print(min('abc')) #a

#獲取字符串中指定字符出現個數

_str = 'aabca'

'''參數:

指定字符

開始位置,默認為 0

結束位置,默認為 len(str)'''

print(_str.count('a')) #3

print(_str.count('a',0,2)) #2

5. 判定字符串是否都是大寫或者小寫字母,返回結果為布爾值

#判定字符或者字符串是否為大寫字母

print('a'.isupper()) #False

print('AB'.isupper()) #True

print('Hello'.isupper()) #False

#判定字符或者字符串是否為小寫字母

print('a'.islower()) #True

print('AB'.islower()) #False

print('Hello'.islower()) #False

拓展:

#判定字符串是否僅包含字母,且非空

print('hello'.isalpha()) #True

print('a b'.isalpha()) #False

print('a123'.isalpha()) #False

#判定字符串是否僅包含字母和數字,且非空

print('hello'.isalnum()) #True

print('a b'.isalnum()) #False

print('a123'.isalnum()) #True

#判定字符串中是否僅包含數字,且非空

print('hello'.isspace()) #False

print('\n \t'.isspace()) #True

print('a123'.isspace()) #False

#判定字符串中是否以大寫字母開頭

print('Hello'.istitle()) #True

print('AB'.istitle()) #False

print('a123'.istitle()) #False

6. 字符串列表連接和分割

#將字符串列表連接起來

_tab = ['This', 'is', 'Python']print(''.join(_tab)) #This is Python

#將字符串分割

msg = 'This is Python'

#默認情況下,按照空白字符分割

print(msg.split()) #['This', 'is', 'Python']#可以指定字符或者字符串

msg = 'This#is#Python'

print(msg.split('#')) #['This', 'is', 'Python']

7. 字符串對齊文本

#左對齊,默認以空格字符代替,可使用其他字符

print('Hello'.ljust(10)) #Hello

print('Hello'.ljust(10,'=')) #Hello=====

#右對齊,默認以空格字符代替,可使用其他字符

print('Hello'.rjust(10)) #Hello

print('Hello'.rjust(10,'=')) #=====Hello

#居中對齊

print('Hello'.center(10)) #Hello

print('Hello'.center(10,'=')) #==Hello===

8. 其他

msg = 'Hello Python'

#判定是否以指定字符或者字符串開始

print(msg.startswith('h')) #False

print(msg.startswith('Hello')) #True

#判定是否以指定字符串或者字符串結尾

print(msg.endswith('N')) #False

print(msg.endswith('on')) #True

字符串格式化

1. 使用字符格式化符號,比如:

_str = 'My Name:%s Weight:%d kg Height: %f cm' % ('Python', 2,2)print(_str) #My Name:Python Weight:2 kg Height: 2.000000 cm

2. 使用format

#不設置指定位置,按照默認順訊來

_str = 'My Name is {} and Weight is {} kg'.format('Python', 2)print(_str) #My Name is Python and Weight is 2 kg

#設置指定位置

_str = 'My Name is {0} and Weight is {1} kg'.format('Python', 2)print(_str) #My Name is Python and Weight is 2 kg

_str= 'My Name is {0} and Weight is {1} kg {0}'.format('Python', 2)print(_str) #My Name is Python and Weight is 2 kg Python

#設置參數

_str = 'My Name is {name} and Weight is {weight} kg'.format(name = 'Python', weight = 2)print(_str) #My Name is Python and Weight is 2 kg

2.1 使用format對數字格式化,有如下幾種:

#保留小數點后幾位

print('{:.3f}'.format(3.14156)) #3.142#帶+號保留小數點后幾位

print('{:+.2f}'.format(-3.0067)) #-3.01#不帶小數

print('{:.0f}'.format(-3.0154)) #-3

print('{:.0f}'.format(3.4654)) #3#以逗號分隔數字

print('{:,}'.format(100000)) #100,000

#顯示百分比

print('{:.2%}'.format(0.256)) #25.60%

#數字補0

print('{:0>2d}'.format(5)) #05 左邊填充,數字寬度為2

print('{:0<3d}'.format(5)) #500 右邊填充,數字寬度為3

#數字補其它

print('{:x>2d}'.format(5)) #x5

print('{:s>2d}'.format(5)) #s5

print('{:#>2d}'.format(5)) ##5

#若 : 后沒有數字或者字符,則以空格填充,可用來對比

print('{:x>10d}'.format(13)) #xxxxxxxx13

print('{:>10d}'.format(13)) #13

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python 字符ab+字符c 2_python入门 之 字符串(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

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