Python strings and usage

Python string is the most commonly used data types
1. # string definition
A = 'westos'
B = "What apos"
C = "" "
User Management System
1. Add User
2. Delete User
3. Display user
.. ...

"""
print(a)
print(b)
print(c)

Python strings and usage

2. Characteristics of string

String is an ordered set of characters, concrete elements can be obtained by its position. In python, the character string is extracted by an index, the index starts from 0.
python may be negative, showing extracts from the end of the last -1, -2 penultimate, i.e. the program can count backwards from that end.
= S 'Hello'
# Index: 01,234 (the index is zero)
Print (S [0])
Print (S [. 4])
Print (S [-1]) # come last character
Python strings and usage

切片可以提取相应部分数据,通常右边界不包括在提取字符串内。
#切片 s[start:stop:step] 从start开始到end -1结束
#步长为step
print(s[0:3])
print(s[0:4:2])
print(s[:]) #显示所有的字符
print(s[:3]) #显示前3个字符
print(s[::-1]) #字符串的翻转
print(s[1:]) #除了第一个字符之外的其他全部字符
Python strings and usage

#重复
格式 : 变量 重复次数
print(s
10)
Python strings and usage

#连接
格式 : 变量1 + 变量2

print('hello ' + 'python')
Python strings and usage

#成员操作符
in : 是成员为真
not in : 不是成员为真
可以判断指定字符是不是包含在字符串中(是否为i组成字符串的成员),输出值为真true和假false

print('he' in s)
print('aa' in s)
print('he' not in s)

Python strings and usage

#for循环遍历
使用for 语句可以循环输出字符串中的每一个字符

for i in s:
print(i)

判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)
读都是一样
的整数。

示例 1:
输入: 121
输出: true
示例 2:
输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。
因此它不是一个回文数。

示例 3:
输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数

num = input('Num:')
print(num == num[::-1])
Python strings and usage

字符串的常用方法

istitle() #判断字符串是否为标题(标题首字母大写) 输出为True 或者False
isupper() #判断字符串是否为全部大写 输出为True 或者False
islower() #判断字符串是否为全部小写 输出为True 或者False
lower() #将字符串转化成小写
upper() #将字符串转化成大写
title() # 将字符串转化成标题格式
endswith('str') #判断是否以str结尾输出结果为True或者False
startswith('str') #判断是否以str开头输出结果为True或者False
isdigit() #判断字符串是否全部由数字组成
isalpha() #判断字符串是否全部由字母组成
isalnum() #判断字符串是全部由字母和数字组成
strip() #去除左右两边的空格,空格为广义的空格 包括:\t \n
lstrip() #去除左边的空格,空格为广义的空格 包括:\t \n
rstrip() #去除右边的空格,空格为广义的空格 包括:\t \n
#同时也可以在括号中指定字符,将左右两边的指定字符串去除

例:

'Hello'.istitle()

爬取网页字符串用法

filename = 'hello.logrrrr'
if filename.endswith('.log'):
print(filename)
else:
print('error.file')

url = 'https://172.25.254.250/index.html'
if url.startswith('http://'):
print('爬取网页')
else:
print('不能爬取')

变量名定义是否合法:
1.变量名可以由字母 数字 下划线组成
2.变量名只能以字母和或者下划线开头
while True:
s = input('变量名:')
if s == 'exit':
print('exit')
break
if s[0].isalpha() or s[0] == '':
for i in s[1:]:
if not(i.isalnum() or i == '
'):
print('%s变量名不合法' %(s))
break
else:
print('%s变量名合法' %(s))
else:
print('%s变量名不合法' %(s))

字符串的搜索和替换.

s = 'hello world hello'
#find找到子字符串,并返回最小的索引
print(s.find('hello'))
print(s.find('world'))
print(s.rfind('hello'))
#替换字符串中的hello为westos
print(s.replace('hello','westos'))

字符串的统计
print('hello'.count('l'))
print('hello'.count('ll')) 统计字符串中l的个数

print(len('westosssss')) 统计多少个字符

字符串的分离和连接

s = '172.25.254.250'
s1 = s.split('.')
print(s1)
print(s1[::-1])

date = '2019-05-24'
date1 = date.split('-')
print(date1)
#连接 通过指定的连接符,连接每个字符串
print(''.join(date1))
print('/'.join(date1))
print('~~'.join('hello'))

小米笔试编程题目

  • 题目描述:

    给定一个句子(只包含字母和空格), 将句子中的单词位置反转,
    单词用空格分割, 单词之间只有一个空格,前>后没有空格。
    比如: (1) “hello xiao mi”-> “mi xiao hello”

  • 输入描述:

    输入数据有多组,每组占一行,包含一个句子(句子长度小于1000个字符)

  • 输出描述:

    对于每个测试示例,要求输出句子中单词反转后形成的句子

  • 示例1:
  • 输入
    hello xiao mi
  • 输出
    mi xiao hello

print(' '.join(input().split()[::-1]))

  1. Design a program to help students practice addition within 10
    details:
    • Adding randomly generated subject;
    • Students view the title and enter the answer;
    • Determine whether students answer correctly?
    • When you exit, the total number of statistics students answer, the correct amount and the correct rate (rounded to two decimal places);

Python strings and usage

Guess you like

Origin blog.51cto.com/12893781/2401060
Recommended