字符串内建函数

find函数查找

strint_example = "hello world good night"
index = strint_example.find("good")
print(index)
12

index函数检索

strint_example = "hello world good night"
index = strint_example.index("good",0,10)#从零开始检索good是否包含在内
print(index)
Traceback (most recent call last):
  File "E:/pycharm202.2.3/pythonProject1/demo1.py", line 2, in <module>
    index = strint_example.index("good",0,10)
ValueError: substring not found

Process finished with exit code 1

如果包含就不报错
strint_example = "hello world good night"
index = strint_example.index("good",1,17)
print(index)
12    #包含运行的结果

count函数基数

strint_example = "hello world good night"
result = strint_example.count("good")
print(result)
1

replace函数替换

strint_example = "hello world good night"
new_string = strint_example.replace("night","evening",1)	#1代表替换次数,不能超过这个次数
print(new_string)
hello world good evening

split函数分隔符

strint_example = "hello world good night"
print(strint_example.split())
print(strint_example.split("e",1))
print(strint_example.split("h"))
['hello', 'world', 'good', 'night']
['h', 'llo world good night']
['', 'ello world good nig', 't']

capitalize首字母大写其他小写

old_string = "hello World Good Night"
new_string = old_string.capitalize()
print(new_string)
Hello world good night

title函数标题化(所有单词都是大写开始,其余均为小写)

old_string = "hello World Good Night"
new_string = old_string.title()
print(new_string)
Hello World Good Night

startwith(检索字符窜是否是指定字符串开头)

old_string = "hello World Good Night"
new_string = old_string.startswith("hello")
print(new_string)
True

endwith检索结尾

old_string = "hello World Good Night"
new_string_one = old_string.endswith("Night")
new_string_tow = old_string.endswith("night")
print(new_string_one)
print(new_string_tow)
True
False

upper函数

old_string = "hello World Good Night"
new_string = old_string.upper()
print(new_string)
HELLO WORLD GOOD NIGHT

ljust(rjust)函数(字符串左对齐)

old_string = "hello World Good Night"
new_string = old_string.ljust(30,'-')	#  宽度为30,单引号里面为剩余位置的情况
print(new_string)
hello World Good Night--------

old_string = "hello World Good Night"
new_string = old_string.rjust(30,'-')
print(new_string)
--------hello World Good Night

lstrip(rstrip)截掉左/右边空格或者指定字符

old_string_one = "     hello World Good Night"
old_string_tow = "hello World Good Night     "
new_string_one = old_string_one.lstrip()
new_string_tow = old_string_tow.rstrip()
print(new_string_one)
print(new_string_tow)
hello World Good Night
hello World Good Night
### Python 字符串内置函数概述 Python 提供了一系列丰富的字符串操作方法,这些方法可以直接用于处理字符串数据。以下是常见的字符串内置函数及其功能说明: #### 基本字符串函数 - **`len(s)`**: 返回字符串 `s` 的长度[^2]。 ```python s = "hello" length = len(s) # 输出为5 ``` - **`find(sub, start=0, end=len(string))`**: 查找子字符串 `sub` 在字符串中的第一个位置,如果找不到则返回 `-1`。 ```python text = "hello world" position = text.find("world") # 输出为6 ``` - **`rfind(sub, start=0, end=len(string))`**: 类似于 `find()` 方法,但它会返回最后一次匹配到的索引位置。 ```python text = "banana" last_position = text.rfind("a") # 输出为5 ``` - **`index(sub, start=0, end=len(string))`**: 和 `find()` 功能相似,但如果未找到子字符串,则抛出异常。 ```python try: index_value = "abc".index("d") except ValueError as e: print(e) # 输出 'substring not found' ``` - **`rindex(sub, start=0, end=len(string))`**: 类似于 `index()`,但返回的是最后出现的位置。 --- #### 其他常用字符串函数 除了上述基本函数外,还有许多其他实用的方法可以用来操作字符串: - **`upper()`**: 将字符串转换成大写形式。 ```python result = "hello".upper() # 结果为'HELLO' ``` - **`lower()`**: 转换成小写形式。 ```python result = "HELLO".lower() # 结果为'hello' ``` - **`strip([chars])`**: 移除字符串两端的空白字符(默认为空格),也可以指定移除特定字符集合。 ```python cleaned_string = " hello ".strip() # 结果为'hello' custom_strip = "www.example.com".strip("wcom.") # 结果为'example' ``` - **`split(sep=None, maxsplit=-1)`**: 根据分隔符分割字符串并返回列表,默认按任意空白字符分割。 ```python words = "apple banana cherry".split() # ['apple', 'banana', 'cherry'] limited_split = "one,two,three,four".split(",", 2) # ['one', 'two', 'three,four'] ``` - **`join(iterable)`**: 使用当前字符串作为连接符来拼接可迭代对象中的元素。 ```python items = ["red", "green", "blue"] joined_text = ",".join(items) # 结果为'red,green,blue' ``` - **`replace(old, new[, count])`**: 替换字符串中的旧子串为新子串,可以选择替换次数。 ```python modified_text = "hello world".replace("world", "there") # 结果为'hello there' partial_replace = "ababa".replace("a", "A", 2) # 结果为'AbaBA' ``` --- #### 数字相关函数 虽然题目提到的内容主要涉及字符串,但也提到了一些通用数值型函数,例如: - **`abs(x)`**: 获取数字的绝对值[^1]。 ```python absolute_value = abs(-42) # 结果为42 ``` 以上列举了一些常用的字符串内置函数以及部分扩展内容,具体应用可以根据实际需求灵活组合使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值