python学习之字符串函数用法

#1、find :返回查找字符串的下标位置。如果返回是-1,代表的是没有查到该字符。rfind :是从右边到左边
username = 'hello yaya'

index = username.find('world')

if index > 0:
print(index)
else:
print('您查找的字符不存在')

#2、index:和find很类似。只不过当查找不到这个字符的时候,不是返回-1,而是抛出一个异常。rindex是从右边开始查找

username = 'hello yaya'

#index = username.index('yaya')

index = username.index('world')
#抛出异常
print(index)

#3、len函数:用来获取字符串的长度

username = 'hello python python'
length =username.len(username)
print(username )

#4、count:用来获取某个字符在字符串中出现的次数

username = 'hello python python'
temp =username.count('h')
print(temp)

#5、replace 函数:新创建一个字符串,把原来字符串中的某个字符替换为你想要的字符串

username = 'hello python python'
new_username = username.replace('python','lecture')
print(username )
print(new_username)

#6、spilt函数:按照给定的字符串进行分割。返回的是一个列表(相当于其他的语言当中的数组)

username = 'hello python python'
username_list = username.split(' ')
print(username_list )


#7、startwith函数:用来判断一个字符串是否以某个字符串开始
username = 'hello world'
result = username.startswith('hello')
print(result)


#判断是否是一个网址

url=('xxxx://www.baidu.com/')
result = url.startswith('http')
if result:
print('this is a website')
else:
print('this is not a website')

#8、endswith函数:用来判断一个字符串是否以某个字符串结束

username = 'hello world'
result = username.endswith('d')
print(result)

#判断用户上传的图片格式

file_name = 'xxx.jf'
if file_name.endswith('jpg') or file_name.endswith('png') or file_name.endswith('gif') :
print('this a picture file')
else:
print('this is not a picture file')


#9、lower函数:将字符串全部改成小写

验证码大小写例子
captcha_server = 'uDV5'
captcha_user = 'udv5'
if captcha_server.lower() ==captcha_user.lower() :
print('验证码正确')
else:
print('验证码错误')


#10、upper函数:将字符串全部改成大写。与lower类似


#11、strip函数:将字符串左右的空格全部去掉

username = ' hello '
username_strip =username.strip()
print(len(username) )
print(len(username_strip ) )
print(username_strip)

电话号码的空格处理
telephone_number = ' 18909875643 '
telephone_number_strip =telephone_number.strip()
print(len(telephone_number ) )
print(len(telephone_number_strip ) )
print(telephone_number_strip )


#12、lstrip函数:删除字符串右边的空格
username = ' hello '
username_lstrip =username.lstrip()
print(len(username) )
print(len(username_lstrip ) )
print(username_lstrip)

#13、rstrip函数:删除字符串右边的空格

username = ' hello '
username_rstrip =username.rstrip()
print(len(username) )
print(len(username_rstrip ) )
print(username_rstrip)

#14、partition:从'str'出现的第一个位置起,把字符串'string'分成一个3元素的
# 元组'(string_pre_str,str,string_post_str)',如果string中不包含'str',
#则'string_pre_str == string '

username = 'python python1 python2 python4'
username_tuple = username.partition('python2')
print(username_tuple )

#15、isalnum:如果string 至少有一个字符并且所有的字符都是字母或数字则返回'True',
#否则返回'false'
username = 'python1234'
result = username .isalnum()
print(result )

#16、isalpha:如果string 至少有一个字符并且所有的字符都是字母则返回'True',
#否则返回'false'
username = 'pythonpython'
result = username .isalpha()
print(result )

#17、isdigit:如果string只包含数字则返回'True',否则返回'false'

username = '1234'
result = username .isdigit()
print(result )


#18、isspace:如果string只包含空格则返回'True',否则返回'false'

username = ' '
result = username .isspace()
print(result )


##19、format:格式化字符串

# 1、format的最简单的用法

name = 'python'
age = 18
greet = 'my name is {},my age is {}'.format(name,age)
print(greet)


# 2、format函数使用位置i参数的方式

name = 'python'
age = 18
greet = 'my name is {0},my username is {0},my age is {1}'.format(name,age)
print(greet)



# 3、关键字参数xx=xxx

name = 'python'
age = 18
greet = 'my name is {username1},my username is {username1},my age is {age1}'.format(username1=name,age1=age)
print(greet)

##以善知识部分字符串函数的方法

#查看所有字符串函数方法

all_methods = dir(str)
print(all_methods )


#转义字符 '\在行尾':续行符

'\n ':换行符

'\'':单引号;'\"':双引号;'\t':制表符;'\\':反斜杠

temp = r'hello \\n world'
print(temp )
#原生字符串 输出原始的字符

猜你喜欢

转载自www.cnblogs.com/godblessmehaha/p/11056321.html