Python字串,列表

來源:互聯網
上載者:User

標籤:python字串 列表

字串:
字串的建立:
單引號, 雙引號 ,三引號 <注意: 逸出字元的使用>
字串的特殊性:
索引 切片 串連 重複 成員操作符(in, not in)

字串的常用方法:
1). 判斷字串的類型(isdigit, isspace, isupper,islower ......)
2). 字串開頭結尾的判斷(endwith,startwith)
endwith -- 多用於尋找指定的檔案格式(.log, .png......)
startwith -- 所用於判斷使用的協議(http://, https://, ftp://)
3). 去除空格(left,middle,right)
lstrip #去除左邊空格
replace (" ", "") # 使用replace函數間接替換中間的空格
rstrip #去除右邊空格
strip #去除左右兩邊空格
4).置中 , 居左, 居右
center
ljust
rjust

    5). 切片    split()    6). 串連    join()

內建函數:
cmp max min enumerate zip sum len abs

數值,字串是不可變資料類型: 列表是可變資料類型

測試練習:
2017-好未來-筆試編程題

  • 題目描述:
    輸入兩個字串,從第一字串中刪除第二個字串中所有的字元。例如,輸入”They are students.”和”aeiou”,則刪除之後的第一個字串變成”Thy r stdnts.”

  • 輸入描述:
    每個測試輸入包含2個字串

  • 輸出描述:
    輸出刪除後的字串

  • 樣本1:
輸入    They are students.    aeiou輸出Thy r stdnts.
#!/usr/bin/env python#coding:utf-8s1 = raw_input ("please input your first string:")s2 = raw_input ("please input your second string:")for i in s2:    s1=s1.replace(i,"")else:    print s1

測試結果:

2017-小米-句子反轉

  • 題目描述:

    給定一個句子(只包含字母和空格), 將句子中的單詞位置反轉,單詞用空格分割, 單詞之間只有一個空格,前後沒有空格。 比如: (1) “hello xiao mi”-> “mi xiao hello”

  • 輸入描述:

    輸入資料有多組,每組佔一行,包含一個句子(句子長度小於1000個字元)

  • 輸出描述:

    對於每個測試樣本,要求輸出句子中單詞反轉後形成的句子

  • 樣本1:
- 輸入    hello xiao mi- 輸出    mi xiao hello

#!/usr/bin/env python
#coding:utf-8
print " ".join(raw_input("please in put your first string:").split()[::-1])

測試結果:![](http://i2.51cto.com/images/blog/201803/18/d07ff4e4a83878f51f96ccfb1c4ada48.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)華為測試:題目描述:輸入一行字串,分別統計出包含英文字母,空格,數字和其他字元的個數

#!/usr/bin/env python
#coding:utf-8
s = raw_input("請輸入字元:")

al_count, space_count, num_count, other_count = 0, 0, 0, 0

for i in s:
if i.isalpha():
al_count += 1
elif i.isspace():
space_count += 1
elif i.isdigit():
num_count += 1
else:
other_count += 1

print "英文字母個數:%d\n空格個數:%d\n數字個數:%d\n其他字元個數:%d" % (
al_count, space_count, num_count, other_count)

測試結果:![](http://i2.51cto.com/images/blog/201803/18/a7fe6d2648487416412ad7562770f7d4.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)枚舉,zip

#!/usr/bin/env python
#coding:utf-8
s = "hello"
for i in range(len(s)):
print i,s[i]

測試結果:![](http://i2.51cto.com/images/blog/201803/18/6265339f9db37d71ce9d78bd435b4ce9.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)enumerate內建函數枚舉;![](http://i2.51cto.com/images/blog/201803/18/1ac49d6feccfad85077d3bf1a037189f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)zip內建函數;![](http://i2.51cto.com/images/blog/201803/18/2b39e4c9cbaa96c517666afb227ec486.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)List 列表列表特性:

#!/usr/bin/env python
#coding:utf-8
array = [1, 2, 3, 4, 5, 6, 7]
li = [1, 1.0, 1L, "hello", 1 + 3j]

#索引:正向索引和反向索引片

print li[::-1]

#重複

print li * 3

#串連

print array + li

#成員操作符

print 1 in li
print 1 not in li

結果輸出:![](http://i2.51cto.com/images/blog/201803/18/ba67f09d4cf8ddb63e4637d06d1f1db0.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)列表增刪查改

#!/usr/bin/env python
#coding:utf-8
allow_ip = [‘172.25.254.91‘, ‘172.25.254.2‘, ‘172.25.254.14‘, ‘172.25.254.32‘]

# 增#append追加元素到列表的最後;

#allow_ip.append(‘172.25.254.6‘)
#print allow_ip

#insert將元素添加到指定索引的前面;

#allow_ip.insert(1,‘172.25.254.34‘)
#print allow_ip

#extend是追加多個元素到列表中;

#allow1_ip = [‘172.25.254.56‘, ‘172.25.254.66‘]
#allow_ip.extend(allow1_ip)
#print allow_ip

#改#給列表的指定索引重新賦值;

#allow_ip[0] = ‘172.25.254.11‘
#print allow_ip

#查#顯示指定元素出現的次數;

#print allow_ip.count(‘172.25.254.1‘)

#顯示指定元素的索引值;如果出現多個,顯示小的那個索引;如果元素不存在,報錯,ValueError;

#print allow_ip.index(‘172.25.254.1‘)

#刪#pop是刪除指定索引的值;如果列表為空白或者索引不在範圍內,則報錯; 如果不指定索引值,預設刪除最後一個元素;#allow_ip.#刪除列表中最先出現的值,不存在,則報錯;

#allow_ip.remove(‘172.25.254.1‘)
#print allow_ip

#反轉列表

#allow_ip.reverse()

#列表排序

allow_ip.sort()
print allow_ip

測試練習:多使用者登陸系統:        1). 已知多個使用者名稱和密碼;        2). 判斷使用者名稱是否存在,                如果登陸的使用者不存在,則報錯, 清註冊使用者;                如果使用者存在, 則判斷密碼是否正確:                    如果正確, 輸出使用者登陸成功;                    如果不正確, 輸出登陸失敗;

#!/usr/bin/env python
#coding:utf-8
import getpass

users = [‘user1‘, ‘user2‘, ‘user3‘]
passwds = [‘123‘, ‘456‘, ‘123‘]

inuser = raw_input("使用者名稱:")
inpass = getpass.getpass("密碼:")

#if 使用者是否存在(成員操作符):
if inuser in users:
index = users.index(inuser)
passwd = passwds[index]
if passwd == inpass:
print "%s登陸成功....." %(inuser)

##1).找出使用者名稱的索引值##2).取出對應使用者名稱的密碼##3).判斷是否一致#if 密碼正確:    #passelse:    print "密碼不正確..."

else:
print "%s 不存在,清註冊....." %(inuser)

測試結果:因為匯入getpass。在測試時必須用終端進行。![](http://i2.51cto.com/images/blog/201803/18/2783a8fae766bf49e8261718bfe51e25.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)測試練習:批量產生學號:學號前4位為1705    依次有三個學院:電子工程學院(01), 軟體學院(02), 商學院(03)    每個學院有200人;#!/usr/bin/env python#coding:utf-8for i in  [‘01‘, ‘02‘, ‘03‘]:    for j in range(1,201):        print "1705%s%.3d" %(i,j)

Python字串,列表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.