Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

str在python中的用法

作者:枫尘于往逝   发布日期:2025-11-18   浏览:39

# str在Python中的用法

# 1. 创建字符串
string_example = "这是一个字符串示例"
print(string_example)  # 输出: 这是一个字符串示例

# 2. 字符串格式化
name = "Alice"
age = 30
formatted_string = f"我的名字是{name},我今年{age}岁。"
print(formatted_string)  # 输出: 我的名字是Alice,我今年30岁。

# 3. 字符串方法
original_string = "hello world"

# 转换为大写
upper_case = original_string.upper()
print(upper_case)  # 输出: HELLO WORLD

# 转换为小写
lower_case = original_string.lower()
print(lower_case)  # 输出: hello world

# 首字母大写
capitalized = original_string.capitalize()
print(capitalized)  # 输出: Hello world

# 查找子字符串
index = original_string.find("world")
print(index)  # 输出: 6

# 替换子字符串
replaced_string = original_string.replace("world", "Python")
print(replaced_string)  # 输出: hello Python

# 4. 字符串连接
string1 = "Hello"
string2 = "World"
combined_string = string1 + " " + string2
print(combined_string)  # 输出: Hello World

# 5. 字符串分割
sentence = "This is a sentence."
words = sentence.split()
print(words)  # 输出: ['This', 'is', 'a', 'sentence.']

# 6. 去除空白字符
whitespace_string = "   前后有空格   "
stripped_string = whitespace_string.strip()
print(stripped_string)  # 输出: 前后有空格

上一篇:python kafka

下一篇:编写一个python程序

大家都在看

python时间格式

python读取文件路径

staticmethod在python中有

python开发windows应用程序

python中len是什么意思

python ord和chr

python中的yield

python自定义异常

python判断路径是否存在

python list.pop

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站