python--字符串切片和常用的写法

  • 正序切片
  • 格式
  • 注意点
  • 倒序切片
  • 格式
  • 字符串运算
  • 字符串转义
  • 字符串常用方法
  • 大小写相关的
  • 统计相关的
  • 拆分&替换
  • 字符串连接(面试)
  • 字符串格式化


正序切片

格式

str1[起始索引:结束索引]左闭右开(取左边下标的值,不取右边下表的值)
str1[起始索引:结束索引:步长]

注意点

1、默认起始位置从0开始,步长是1
2、左闭右开(取起始索引的值,不取结束索引的值)
3、如果结束索引不写,默认等于字符串的长度
4、空格也占用索引(一个空格占一位)
5、起始索引绝必须要小于结束索引
6、倒序下次课将,先把正序搞定

倒序切片

格式

str1[起始索引:结束索引:步长]

字符串运算

字符串转义

1、\n:表示换行符,通常用于在字符串中表示文本的不同行。
2、\:是转义字符,它用于将特殊字符转义为普通字符,或者将一个长的代码行分成多行书写
3、r:表示原始字符串的前缀,通常用于在字符串中包含反斜杠等特殊字符时,避免被当做转义字符处理。例如,r"\n" 表示字符串"\n",而不是换行符。

python 字符串根据 空格分组 python字符串空格切片_数据库

字符串常用方法

大小写相关的

str_01.upper():全部大写
str_02.lower():全部小写
#了解
str_03.capitalize():首字母大写
str_04.title():每个单词首字母大写
str_05.swapcase():大小写互换

统计相关的

  • count():统计字符在字符串中出现的次数

sub:需要统计的字符
start:统计范围的开始索引值
end:统计范围的结束索引值
返回:sub在字符串中出现的次数

str_1 = 'hello python'
times = str_1.count('h')
print(times)
# 代码格式化快捷键:ctrl + alt + l
# 看源码:ctrl + 点击一下
    def count(self, sub, start=None, end=None): 
        """
        S.count(sub[, start[, end]]) -> int
        Return the number of non-overlapping occurrences of substring sub in
        string S[start:end].  Optional arguments start and end are
        interpreted as in slice notation.
        """
        return 0
  • find():查找字符在字符串中的索引位置

sub:需要统计的字符
start:统计范围的开始索引值
end:统计范围的结束索引值
默认只查询第一次找到的字符,然后返回索引值

str_1 = 'hello python'
index = str_1.find('h')
print(index)
# 代码格式化快捷键:ctrl + alt + l
# 看源码:ctrl + 点击一下
    def find(self, sub, start=None, end=None): 
    # real signature unknown; restored from __doc__
        """
        S.find(sub[, start[, end]]) -> int
        
        Return the lowest index in S where substring sub is found,
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.
        
        Return -1 on failure.
        """
        return 0

拆分&替换

拆分(面试用):
split():字符串拆分,默认是通过空格键拆分,可以自己指定拆分字符,拆分的过程会讲指定的拆分字符丢弃
maxsplit参数作用控制拆分次数,默认(-1)不限制拆分次数,负数表示不限制拆分次数,如果是0就不进行拆分

# 不加maxsplit参数,默认不限制拆分次数
str_1 = 'hellopython'
new_str=str_1.split('o')
print(new_str)

python 字符串根据 空格分组 python字符串空格切片_python 字符串根据 空格分组_02

# 不加maxsplit参数,默认不限制拆分次数
str_1 = 'hellopython'
new_str=str_1.split('o',1)
print(new_str)

python 字符串根据 空格分组 python字符串空格切片_服务器_03

替换:
replace()
第一个参数:需要替换的字符(你要换掉谁)
第二个参数:替换后的字符(你要换成谁)
第三个参数:替换次数(你要替换多少次)
可以替换任意字符包括空格键
返回:替换后的新字符串

str_1 = 'hello python'
new_str=str_1.replace('o','8',1)
#new_str=str_1.replace('o','8')
print(new_str)

python 字符串根据 空格分组 python字符串空格切片_python 字符串根据 空格分组_04

str_1 = 'hello python'
#new_str=str_1.replace('o','8',1)
new_str=str_1.replace('o','8')
print(new_str)

python 字符串根据 空格分组 python字符串空格切片_字符串_05

字符串连接(面试)

new_str=''.join(str_1)
通过制定的字符串(
)连接str_1

# h-e-l-l-o-p-y-t-h-o-n  面试题
str_1 = 'hellopython'
new_str='*'.join(str_1)
print(new_str)

python 字符串根据 空格分组 python字符串空格切片_python_06

字符串格式化

1、%
%s:字符串占位符,如果传的不是字符串,会发生强制类型转换
%d:int类型占位符
2、{}

# %s:占位符  s:str
test_str='this is test %s' %('py39')
print(test_str,type(test_str))

python 字符串根据 空格分组 python字符串空格切片_字符串_07

# % d  int类型占位符
# 参数化
test_str='this is a number %d' %(100)
print(test_str)

python 字符串根据 空格分组 python字符串空格切片_服务器_08