String:
The creation of a string:
Single quotes, double quotes, triple quotes < note: Use of escape characters >
The particularity of the string:
Index tile join repeating member operator (in, not in)
Common Methods for strings:
1). Determine the type of string (IsDigit, Isspace, Isupper,islower ...). )
2). Judgment of the beginning and end of the string (Endwith,startwith)
Endwith--is used to find the specified file format (. log,. png ...). )
Startwith-the protocol used to determine the use (http:/, https://, ftp://)
3). Remove Space (left,middle,right)
Lstrip #去除左边空格
Replace ("", "") # indirectly replaces the middle space with the Replace function
Rstrip #去除右边空格
Strip #去除左右两边空格
4). Center, left, right
Center
Ljust
Rjust
5). 切片 split() 6). 连接 join()
Built-in functions:
CMP Max min Enumerate zip sum len Abs
numeric value, string is immutable data type: List is mutable data type
Test exercise:
2017-Good future-written test programming questions
Title Description:
Enter two strings, removing all the characters from the second string from the first string. For example, enter "they is students." and "Aeiou", the first string after deletion becomes "Thy R stdnts."
Input Description:
Each test input consists of 2 strings
Output Description:
Output the deleted string
- Example 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
Test results:
2017-millet-sentence reversal
Title Description:
Given a sentence (containing only letters and spaces), the word position in the sentence is reversed, the word is separated by a space, there is only one space between the words, and there are no spaces. For example: (1) "Hello Xiao Mi", "Mi xiao Hello"
Input Description:
There are multiple groups of input data, one row for each group, with a sentence (sentence length less than 1000 characters)
Output Description:
For each test example, it is required to output sentences that are formed after the word reversal in the sentence
- Example 1:
- 输入 hello xiao mi- 输出 mi xiao hello
#!/usr/bin/env python
#coding: Utf-8
Print "". Join (Raw_input ("Please 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 ("Please enter character:")
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 "Number of letters:%d\n spaces:%d\n Number:%d\n number of other characters:%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 ("User name:")
Inpass = Getpass.getpass ("Password:")
#if whether the user exists (member operator):
If Inuser in users:
index = Users.index (Inuser)
passwd = Passwds[index]
if passwd = = Inpass:
Print "%s landed successfully ..."% (Inuser)
##1).找出用户名的索引值##2).取出对应用户名的密码##3).判断是否一致#if 密码正确: #passelse: print "密码不正确..."
Else
Print "%s does not exist, clear registration ..."% (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 string, List