Python基础之字符串操作

字符串基本使用

引用类型

字符串的定义

Str1=”hello1”

Str2=’hello2’

Str=”\n”

字符串的输出

%s

字符串的输入

Str=input(“从键盘录入任意字符串”)

字符串求长度

长度=len(变量/常量)

字符串的索引值

范围:

{0,len(字符串)-1}
str1="\n"
print(type(str1))
print(str1+"javaxl")
str2="123"
print("%s"%str2)
str3="http://javaxl.com"
# 求字符串长度
print(len("str3"))
print(len(str3))

str4='http://javaxl.com'
print(str4[0])
print("javaxl的长度:%d" % len(str4))
#越界 IndexError: string index out of range
# print(str4[23])
# 获取最后一个
print(str4[-1])
print(str4[4])
print(str4[len(str4) - 1])

循环打印出str字符串中的内容

# 使用循环完成字符串中每个自负的打印,使用\t隔开(1、while 2、for)
i=0
while i<len(str4):
    print(str4[i],end='\t')
    i+=1

print()
for i in range(len(str4)):
    print(str4[i],end='\t')

print()
for i in str4:
    print(i,end='\t')

注意:最后一种的写法是String字符串所特有的

字符串的切片操作

字符串的切片操作

字符串变量名(start:end:step)
不包含end

str_num="0123456789"
# 从开始位置截取两个字符出来
res=str_num[0:2]
print(res)
# 切 从0到2的位置,不包括2
# res=str_num[:2]
res=str_num[:2:1]
print(res)
# 取从(0,2)步长为2
res=str_num[:2:2]
print(res)
# 默认取所有
res=str_num[:]
print(res)

# 从索引值位置2的位置开始切片
res = str_num[2:]
print(res)

# 手动切片,从2到末尾
res = str_num[2:len(str_num)]
print(res)

# 切片不会报越界异常 IndexError
res = str_num[2:100]
print(res)

# 获取13579
res = str_num[1::2]
print(res)

# 字符串倒转:9876543210
res = str_num[::-1]
print(res)

# step为负数,表示从右往左取,如果要指定开头和结尾,那么开头要比结尾值大 753
res = str_num[7:1:-2]
print(res)

字符串常用操作

dir

当你给dir()提供一个模块名字时,它返回在那个模块中定义的名字的列表。当没有为其提供参数时, 它返回当前模块中定义的名字的列表。

find/rfind

从左往右/从右往左 找对应子字符串的下标,没有则返回-1

count

子字符串出现的次数,没有出现过则为0

replace

替换子串

split/splitlines

切割/按\n进行切割,返回list列表

capitalize

字符串首字母大写

title

字符串中所有单词首字母大写

startswith/endswith

以…开始/结束

upper/lower

整个字符串转为大写/小写

ljust/rjust/center

从左往右/从右往左/从中间到两边扩充字符串长度

lstrip/rstrip/strip

去除左侧/右侧/所有空格

partition/rpartition

从左往右/从右往左切片,返回元组,保留被切的子串

isalpha/isdigit/isalnum/isspace

是否全为字母/数字/字母或者数字/space

join

str1.join(str2)

用str1将将str2中的每个字符相连

# 当你给dir()提供一个模块名字时,它返回在那个模块中定义的名字的列表。当没有为其提供参数时, 它返回当前模块中定义的名字的列表。
res = dir(str)
print(res)
str1='good good study,day day up'
str2='you can you up,no can no B b'

# -1
print(str2.find('田'))
# 15(从左往右数no的下标)
print(str2.find('no'))
# 22(从右往左的no的下标)
print(str2.rfind('no'))

# no出现的次数
print(str2.count('no'))
# Me出现的次数
print(str2.count('Me'))


# I can you up,no can no B b
str3=str2.replace('you','I',1)
# I can I up,no can no B b
str3=str2.replace('you','I')
print(str3)

# 一个list列表
# ['you', 'can', 'you', 'up,no', 'can', 'no', 'B', 'b']
str4=str2.split(' ')
print(str4)

# ['you can you up,no ', ' can no B b']   遇到\n进行切割
str5="you can you up,no \n can no B b".splitlines()
print(str5)

# 将首字母大写
# You can you up,no can no b b
str6=str2.capitalize()
print(str6)

# 将所有单词的首字母大写
# You Can You Up,No Can No B B
print(str2.title())

url1='http://javaxl.com'
print(url1.startswith('http://'))
print(url1.startswith('https://'))

fileName='javaxl.txt'
print(fileName.endswith('.py'))

str3='JavaXl'
# JAVAXL    全部大写
print(str3.upper())
# javaxl    全部小写
print(str3.lower())

str4='123abc'
# 123abc
print(str4.ljust(10))
# 123abc****    ljust从左边开始扩充,没有值的补*
print(str4.ljust(10, '*'))
# ****123abc    rjust从右边开始扩充,没有值的补*
print(str4.rjust(10, '*'))
# **123abc**    从中间开始,两边同时扩充,没有值的补*
print(str4.center(10, '*'))

str5='   javaxl   '
# javaxl    去掉左侧空格
print(str5.lstrip())
#    javaxl 去掉右侧空格
print(str5.rstrip())
# javaxl    去掉所有空格
print(str5.strip())

str1='ni hao ma hao de hen'
# ('ni ', 'hao', ' ma hao de hen')
# 元组    从左往右切,保留切割点
print(str1.partition('hao'))
# ('ni hao ma ', 'hao', ' de hen')
# 元组    从右往左切,保留切割点
print(str1.rpartition('hao'))
# ['ni ', ' ma ', ' de hen']
# 列表    不保留切割点
print(str1.split('hao'))

str2='abc123'
# False 所有字符都是字母
print(str2.isalpha())
# False 所有字符都是数字
print(str2.isdigit())
# True 所有字符都是数字或者字母
print(str2.isalnum())
# True  所有字符是否都是space
print("    ".isspace())
# False
print("  *  ".isspace())

# a_b_c_1_2_3   用_将str2中的所有字符连接起来
print("_".join(str2))

谢谢大家,多多指教!!!
在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值