python中的字符串

字符串的定义

a = ‘hello’
b = “python”
c = “”"
用户管理系统
1.添加用户
2.删除用户
3.显示用户
“”"
print(type(a))
print(type(b))
print(type©)
print(a)
print(b)
print©
字符串常用的转义符号
“”"
\n:换行
\t:一个tab键
"

“”"
打印guido’s
打印"hello guido’s python"
print(‘guido’s’)
print(“guido’s”)
print(’“hello guido’s python”’)
print("“hello guido’s python”")
print(’%s\n%s’ %(a,b))
print(’%s\t%s’ %(a,b))

字符串的特性

s = ‘hello’
索引:0,1,2,3,4 索引是从0开始的
print(s[0])
print(s[4])

#拿出字符串的最后一个字符
print(s[-1])

#切片
s[start?step] 从start开始,到end-1结束,步长为step(默认是1)
print(’~~~~~~~~~~~~~~~~~~~~~~~~~~’)
print(s)
print(s[0:3])
print(s[0:4:2])

显示所有字符
print(s[:])
显示前3个字符
print(s[:3])
字符串倒序输出
print(s[::-1])
除了第一个字符之外,其他的全部显示
print(s[1:])

#重复
print(s*10)

#连接
print('hello '+‘world’)

#成员操作符
print(‘he’ in s)
print(‘aa’ in s)
print(‘he’ not in s)

字符串的应用

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

## 示例:
#
#示例 1:
#        输入: 121
#        输出: true
#示例 2:
#        输入: -121
#        输出: false
#        解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
#
#示例 3:
#        输入: 10
#        输出: false
#        解释: 从右向左读, 为 01 。因此它不是一个回文数
num = input('Num:')
print(num == num(::-1))

#不用字符串的形式,用整数来判断?

字符串开头和结尾匹配

filename='hello.logggh'
if filename.endswith('.log'):
	print(filename)
else:
	print('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('爬取网页')
else:
	print('不能爬取网页')

字符串去掉两边的空格

In [1]: s = '      hello'                                                      

In [2]: s.strip()                                                              
Out[2]: 'hello'

In [3]: s = '      hello      '                                                

In [4]: s.strip()                                                              
Out[4]: 'hello'

In [5]: s.lstrip()                                                             
Out[5]: 'hello      '

In [6]: s.rstrip()                                                             
Out[6]: '      hello'

In [7]: s = '\nhello      '                                                    

In [8]: s.strip()                                                              
Out[8]: 'hello'

In [9]: s = '\thello      '                                                    

In [10]: s.strip()                                                             
Out[10]: 'hello'

In [11]: s = 'helloh'                                                          

In [12]: s.strip('h')                                                          
Out[12]: 'ello'

In [13]: s.strip('he')                                                         
Out[13]: 'llo'

In [14]: s.lstrip('he')                                                        
Out[14]: 'lloh'

In [15]: s.rstrip('he')                                                        
Out[15]: 'hello'

In [17]: print('学生管理系统'.center(50,'*'))                                  
**********************学生管理系统**********************

In [18]: print('学生管理系统'.ljust(50,'*'))                                   
学生管理系统********************************************

In [19]: print('学生管理系统'.rjust(50,'*'))      

字符串的搜索和替换

find:
replace:
count:

In [20]: s = 'hello python,learn python'                                       

In [21]: s.find('python')                                                      
Out[21]: 6

In [22]: s.rfind('python')                                                     
Out[22]: 19

In [23]: s.replace('python','linux')                                           
Out[23]: 'hello linux,learn linux'

In [24]: s1 = s.replace('python','linux')                                      

In [25]: s1                                                                    
Out[25]: 'hello linux,learn linux'

In [26]: s                                                                     
Out[26]: 'hello python,learn python'

In [27]: s.count('python')                                                     
Out[27]: 2

In [28]: s.count('p')                                                          
Out[28]: 2

In [29]: s.count('i')                                                          
Out[29]: 0

字符串的分离和拼接

split:
join:

In [30]: ip = '172.25.254.10'                                                  

In [31]: ip1 = '1172.25.254.10'                                                

In [32]: ip1.split('.')                                                        
Out[32]: ['1172', '25', '254', '10']

In [33]: date = '2018-11-18'                                                   

In [34]: date.split('-')                                                       
Out[34]: ['2018', '11', '18']

In [35]: date.split('.')                                                       
Out[35]: ['2018-11-18']

In [37]: date.replace('-','/')                                                 
Out[37]: '2018/11/18'

In [38]: ip = ['1172', '25', '254', '10']                                      

In [39]: ''.join(ip)                                                           
Out[39]: '11722525410'

In [40]: ':'.join(ip)                                                          
Out[40]: '1172:25:254:10'

In [41]: '*'.join(ip)                                                          
Out[41]: '1172*25*254*10'

字符串常用方法—大小写

# 判断字符串 变成‘标题’
In [1]: 'Hello'.istitle()
Out[1]: True
In [2]: 'hello'.istitle()
Out[2]: False

In [7]: 'heLLo'.islower()
Out[7]: False
In [8]: 'heLLo'.isupper()
Out[8]: False


# 将字符串全部变为大写
In [3]: 'hello'.upper()
Out[3]: 'HELLO'

# 将字符串全部变为小写
In [4]: 'heLLo'.lower()
Out[4]: 'hello'

In [5]: 'heLLo'.title()
Out[5]: 'Hello'

In [6]: 'heLLo'.swapcase()
Out[6]: 'HEllO'



"""
[[:digit:]] [[:alnum:]]
[[:upper:]] [[:lower:]]
[[:space:]]
"""

字符串判断练习-变量名是否合法变量名是否合法?

1.变量名可以由字母,数字或下划线组成
2.变量名只能以字母或下划线开头

s = ‘hello@’
1.判断变量名的第一个元素是否为字母或下划线: s[0]
2.如果第一个元素符合条件,判断除了第一个元素的其他元素:s[1:]

# for循环:依次遍历字符串的每一个元素
#for i in 'hello':
#	if i.isalpha():
#		print(i)

1.变量名的第一个字符是否为字母或下划线
2.如果是,继续判断(4)
3.如果不是,报错,不合法
4.依次判断除了第一个字符之外的其他字符
5.判断这个字符是否为数字或下划线

while True:
	s = input('变量名:')
	if s == 'exit':
		print('欢迎下次使用')
		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('变量名不合法')







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值