文章目录
1. 字符串的表示
(1)单引号表示
a='hello'
(2)双引号表示
b="what's"
(3)块注释表示
c= """
1.姓名
2.年龄
3.地址
...
"""
2. 字符串的特性
s = 'hello'
s1= "python"
字符以0开始,0,1,2,3…
s[1] | 字符串s中第二个字符 |
s[4] | 字符串中第4个字符 |
s[-4] | 倒数第4个字符 |
s[2:4] | 第[2,4)字符 |
s[0:4:2] | 第[0,4)字符,布长为2 |
s[:] | 字符串所有字符 |
s[:4] | 字符串的前四个字符 |
s[:-1] | 最后一个字符之前的所有字符 |
s[4:] | 字符串前四个字符之后的字符 |
s[::-1] | 将字符串反转 |
s[2] * 10 | 对第二个字符重复操作十次 |
s * 10 | 对字符串重复操作十次 |
"hello " + “python” | 连接字符串“hello”和“python” |
s+s1 | 连接字符串s和字符串s1 |
3. 字符串的成员操作符
print('he' in s)
判断“lo”字符串是否在s中;若存在返回值为Trueprint('py' in s)
判断“py”字符串是否在s中;若不存在返回值为Falseprint ('py' not in s)
判断“py”字符串是否不在s中;不在返回值True;在返回Faluse
4. 字符串的循环遍历
for i in s:
print(i) 对字符串实现循环 #按照字符串顺序循环输出字符串中所有字符
5. 字符串的对齐(center|ljust|rjust)
print('hello'.center(30))
在30个字符长度下,将“hello”显示在正中间print('hello'.ljust(20))
在20个字符长度下,将“hello”显示在左侧
print('hello'.center(30, '+'))
在30个字符长度下,将“hello”显示在正中间,其余空位用‘+’填充print('hello'.ljust(30, 'a'))
在30个字符长度下,将“hello”显示在左侧;其余空位用‘a’字符填充print('hello'.rjust(20, '#'))
在20个字符长度下,将“hello”显示在左侧;其余空位用‘#’字符填充
6. 查找子字符串,并返回最小索引(find)
s = 'hehelloworld'
-
print(s.find('he'))
在字符串s中查找‘he’子字符串,若存在多个‘he’子字符串,返回最小的索引;若不存在返回-1 -
print(s.find('w'))
在字符串s中查找‘w’子字符串,与上同
-
print(s.rfind('he'))
在字符串s中查找‘he’子字符串,若存在返回s中最右侧索引 -
无lfind模块
7. 字符串中的替换(replace)
print(s.replace('he', 'eh'))
将字符串s中的’he’子字符串替换为’eh’;若字符串s中不存在要替换掉的子字符串,字符串s将不做替换
8. 字符串中的计数(count|len)
print(s.count('l'))
对字符串s中的字符’l’计数print('people'.count('p'))
对’people’中的’p’计数
print(len(s))
计算字符串s的长度print(len('xiongyong'))
计算字符串’xiongyong’的长度
9. 字符串的分离与连接(split|join)
(1)分离:
s = 'alonealone'
s1 = s.split('n') # 将字符串以‘n’为分隔符分离
print(s1)
a = '172.25.254.1'
a1 = a.split('.') # 将字符串a以‘.’为分隔符分离
print(a1)
print(a1[::-1])
(2)连接:
print(''.join(a1)) #将字符串a1连接
print('-'.join(s1)) #将字符串s1用‘-’连接
print('&'.join("hello")) #将字符串“hello”用‘&’连接
10. 字符串的判断及字符串的转换(isdigit|isalpha|isalnum|istitle|isupper|islower)
(1)字符串的字母数字判断(isdigit|isalpha|isalnum)
[[:digit:]] | 任意数字字符 |
[[:alpha:]] | 任意字母字符 |
[[:alnum:]] | 任意数字字母字符 |
- 只要有一个元素不满足,就返回False
print('weeeqwe32131'.isdigit())
判断字符串中的字符是否全为数字;是返回True;否则返回Flaseprint('42131321fsas'.isalpha())
判断字符串中的字符是否全为字母;返回结果同上print('weewqeq323213'.isalnum())
判断字符串中的字符是否为字母和数字组成
(2)字符串的字母类型判断(istitle|isupper|islower)- 标题:首字母为大写字母,其余字母为小写字母的字符串
istitle | 判断字符串是否为标题 |
isupper | 判断字符串是否为大写字母 |
islower | 判断字符串是否为小写字母 |
'Hello'.istitle()
判断’Hello’是否为标题;若是,返回值为True;否则,返回值为False
'hello'.isupper()
判断’hello’字符串中字符是否为大写字母;若是返回值为True;否则返回值为False
'hello'.islower()
判断’hello’字符串中字符是否为小写字母;返回值与上同
(3)字符串的转换
-
'Hello'.lower()
将字符串中的所有字符转换为小写字母 -
'hello'.upper()
将字符串中的所有字符转换为大写字母 -
'hello'.title()
将字符串转换为标题格式
11. 字符串中字符的去除(strip)
注意:去除左右两边的空格,空格为广义的空格 包括:\t \n
>>> s=' hello' ##tab键
>>> s
'\thello'
>>> s=' hello' ##空格键
>>> s
' hello'
>>> s.lstrip() ##去除字符串左边的空格
'hello'
>>> s.rstrip() ##去除字符串右边的空格
' hello'
>>> b=' hello '
>>> b
' hello '
>>> b.strip() ##去除字符串中所有空格
'hello'
>>> b.lstrip()
'hello '
>>> b.rstrip()
' hello'
>>> c=' hello\n'##换行
>>> c
' hello\n'
>>> print(c)
hello
>>> c.strip() ##去除所有空格包括\t
'hello'
>>> c.lstrip()
'hello\n'
>>> s='helloh'
>>> s
'helloh'
>>> s.strip('h')##去除字符串中的‘h’字符
'ello'
>>> s.lstrip('h')##去除字符串中左边的‘h’字符
'elloh'
>>> s.rstrip('h')##去除右边的‘h’字符
'hello'
>>> s=' helloh '##在有空格的情况下,去除指定字符不能实现
>>> s
'\thelloh '
>>> s.strip('h')
'\thelloh '
>>> s.lstrip('h')
'\thelloh '
例题:
-
变量名定义是否合法:
(1)变量名可以由字母 数字 下划线组成
(2)变量名只能以字母或者下划线开头s = ‘321csv_’ s[0] s[1:]
s = ‘asfasf%%’
‘’
exit
“”"
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))
2.小米笔试编程题目
- 题目描述:
给定一个句子(只包含字母和空格), 将句子中的单词位置反转,
单词用空格分割, 单词之间只有一个空格,前>后没有空格。
比如: (1) “hello xiao mi”-> “mi xiao hello”
- 输入描述:
输入数据有多组,每组占一行,包含一个句子(句子长度小于1000个字符)
- 输出描述:
对于每个测试示例,要求输出句子中单词反转后形成的句子
- 示例1:
- 输入
hello xiao mi
- 输出
mi xiao hello
"""
print(' '.join(input().split()[::-1]))