文章目录
前言
string(格式)
- 例:
a = ‘hello’
b = ‘what’s up’
c = “”"
用户管理系统
1.添加用户
2.删除用户
3.显示用户
“”"
print(b)
print©
print(type©)
显示结果:
what's up
用户管理系统
1.添加用户
2.删除用户
3.显示用户
<type 'str'>
string(索引,切片,重复,连接,判断)
-
index索引
s = ‘hello’
print(s[0])h
print(s[1])
e
print(s[2])
l
-
cut切片
print(s[0:3]) #s[start:end-1]hel
print(s[0:4:2])
hl
print(s[:])
hello
print(s[:3])
hel
print(s[1:])
ello
print(s[::-1])
olleh
-
repeat重复
print(s * 2)hellohello
-
link连接
print(‘hello’ + ’ world’)hello world
-
judge判断
print(‘h’ in s)True
print(‘f’ in s)
False
string(练习:回文)
示例 1:
输入: 121
输出: true
示例 2:
输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不
是一个回文数。
示例 3:
输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。
num = input('Num:')
if num == num[::-1]:
print('ok')
else:
print('failed')
string(判断字符串属性)
-
num
print(‘123’.isdigit())True
print(‘123abc’.isdigit())
False
-
title
print(‘Hello’.istitle())True
print(‘HelLo’.istitle())
False
-
words
print(‘hello’.upper())HELLO
print(‘HeLlO’.lower())
hello
print(‘hello’.islower())
True
print(‘HELLO’.isupper())
True
print(‘HELL1’.isalnum())
True
print(‘123’.isalpha())
False
print(‘qqq’.isalpha())
True
-
filename(endswith,startswith)
filename = ‘hello.loggg’
if filename.endswith(’.log’):
print(filename)
else:
print(‘error file’)error file
url1 = ‘file:///mnt’
url2 = ‘ftp://172.25.254.250/pub’
url3 = ‘http://172.25.254.250/index.html’if url3.startswith(‘http://’):
print(‘ok’)
else:
print(‘failed’)ok
string(练习:判断)
变量名是否合法:
1.变量名可以由字母,数字或者下划线组成
2.变量名只能以字母或者下划线开头
s = ‘hello@’
1.判断变量名的第一个元素是否为字母或者下划线 s[0]
2.如果第一个元素符合条件,判断除了第一个元素之外的其他元素s[1:]
while True:
s = input('Str:')
if s[0].isalpha() or s[0] == '_':
for i in s[1:]:
if not i.isalnum or i == '_':
print('illegal')
break
else:
print('ok')
else:
print('illegal!')
string(find,replace,count,split,join)
s = ‘hello world hello’
-
find
print(s.find(‘hello’))0
print(s.find(‘world’))
6
-
rfind
print(s.rfind(‘hello’))12
-
replace
print(s.replace(‘hello’,‘westos’))westos world westos
print(‘System Admin’.center(30))
System Admin
print(‘System Admin’.center(30,’*’))
*********System Admin*********
print(‘System Admin’.ljust(30,’*’))
System Admin******************
print(‘System Admin’.rjust(30,’*’))
******************System Admin
-
count
print(‘hello’.count(‘l’))2
print(‘hello’.count(‘ll’))
1
print(len(‘hello’))
5
-
split,join
s = ‘172.25.254.250’
s1 = s.split(’.’)
print(s1)['172', '25', '254', '250']
print(s1[::-1])
['250', '254', '25', '172']
date = ‘2019-04-15’
date1 = date.split(’-’)
print(date1)['2019', '04', '15']
print(’’.join(date1))
20190415
print(’/’.join(date1))
2019/04/15
string(练习:出勤)
给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符>:
‘A’ : Absent,缺勤
‘L’ : Late,迟到
‘P’ : Present,到场
如果一个学生的出勤纪录中不超过一个’A’(缺勤)并且不超过两个连续的’L’(迟到),
那么这个学生会被奖赏。
你需要根据这个学生的出勤纪录判断他是否会被奖赏。
示例 1:
输入: “PPALLP”
输出: True
示例 2:
输入: “PPALLL”
输出: False
s = input()
if s.count('A') <= 1 and s.count('LLL') == 0:
print(True)
else:
print(False)
或:
s = input()
print(s.count('A') <= 1 and s.count('LLL') == 0)
string(练习:句子逆序)
题目描述:
给定一个句子(只包含字母和空格), 将句子中的单词位置反转,单词用空格分割, 单词之间只有一个空格,前后没有空格。
比如: (1) “hello xiao mi”-> “mi xiao hello”
输入描述:
输入数据有多组,每组占一行,包含一个句子(句子长度小于1000个字符)
输出描述:
对于每个测试示例,要求输出句子中单词反转后形成的句子
示例1:
输入
hello xiao mi
输出
mi xiao hello
li = input().split()
print(li)
print(li[::-1])
print(' '.join(li[::-1]))
或:
print(' '.join(input().split()[::-1]))
string(练习:排除被包含字符)
题目描述:
输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,>输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”
输入描述:
每个测试输入包含2个字符串
输出描述:
输出删除后的字符串
示例1:
输入
They are students.
aeiou
输出
Thy r stdnts.
s1 = input('s1:')
s2 = input('s2:')
s3 = ''
for i in s1:
for q in s2:
if q == i:
break
else:
s3 += i
print(s3)
string(练习:小学生习题)
设计一个程序,帮助小学生练习10以内的加法
详情:
- 随机生成加法题目;
- 学生查看题目并输入答案;
- 判别学生答题是否正确?
- 退出时, 统计学生答题总数,正确数量及正确率(保留两>位小数点);
import random
count = 0
right = 0
while True:
a = random.randint(0,9)
b = random.randint(0,9)
print('%d+%d=' %(a,b))
question = input('Please input your answer:(q for exit)')
result = a + b
if question == str(result):
print('OK!')
right += 1
count += 1
elif question == 'q':
break
else:
print('Failed!')
count += 1
percent = right / count
print('测试结束,共回答%d道题,正确个数为%d,正确率为%.2f%%' %(count,right,percent * 100))