python 字串一

本文详细介绍了Python中字符串的基本操作,包括打印、判断空字符串、获取长度、子串提取、反转、截取、遍历等。通过实例演示了如何使用切片、循环和列表反转实现字符串操作,并探讨了重要的编程概念如Slicing和数据结构利用。
摘要由CSDN通过智能技术生成

目录

打印字串

例子1 基本

例子2 带分隔符

例子3 不同结尾

是否为空

例子1 字串为条件

例子2 是否等于

获取长度

例子1 获得长度

例子2 获得空字串长度

子串

例子1 找

例子2 结束位置大于长度

例子3 负位置

例子4 没有开始或结束

反转

例子1 Slicing

例子2 For Loop

例子3 While Loop

例子4 List.reverse()

截取

例子1 结束位置

例子2 开始和结束位置

例子3 开始,结束和步进

遍历

例子1 遍历

例子2 清空并遍历


打印字串

例子1 基本

print('hello world')
Hello World

例子2 带分隔符

print('hello', 'world', sep='-')
Hello-World

例子3 不同结尾

print('hello', 'world',end='.')
Hello World.

是否为空

例子1 字串为条件

my string = ""

if not mystring:

        print("the string is empty.")

else:

        print("the string is not empty.')
The string is empty.

例子2 是否等于

mystring = ""

if mystring == "":

        print("the string is empty.")

else:

        print("the string is not empty.")
The string is empty.

获取长度

例子1 获得长度

mystring = 'python examples'

length = len(mystring)

print('Length of the string is:', length)
Length of the string is: 15

例子2 获得空字串长度

mystring = ''

length = len(mystring)

print('length of the is:', length)
length of the is: 0

子串

例子1 找

mystring = 'pythonexamples.org'

substring=mystring[6:12]

print(substring)
exampl

例子2 结束位置大于长度

mystring = 'pythonexamples.org'

substring = mystring[6:35]

print(substring)
examples.org

例子3 负位置

mystring = 'pythonexamples.org'

substring = mystring[-15:-5]
honexample#mystring[18-15:18-5] where length of mystring is 18

例子4 没有开始或结束

mystring = 'pythonexamples.org'

substring = mystring[:]

print(substring)
pythonexamples.org#original

反转

例子1 Slicing

str = 'Welcome to Python Examples.'

reversed = str[::-1] #from start to end, -1: reverse

print(reversed)

 

Python Reverse String

例子2 For Loop

str = 'Welcome to Python Examples.'

reversed = ''

for c in str:

        reversed = c + reversed #append in reverse order(left)

print(reversed)
D:\>python example.py
.selpmaxE nohtyP ot emocleW

例子3 While Loop

str = 'Welcome to Python Examples.'

reversed = ''

length = len(str) - 1

while length >= 0:

        reversed = reversed + str[length]

        length = length - 1

print(reversed)
D:\>python example.py
.selpmaxE nohtyP ot emocleW

例子4 List.reverse()

str = 'Welcome to Python Examples.'

str_list = list(str)

str_list.reversed()

reversed = ''.join(str_list)

print(reversed)
D:\>python example.py
.selpmaxE nohtyP ot emocleW

截取

例子1 结束位置

string1 = 'hello-world'

stop = 5#end position

slice_object = slice(stop)  #[0,1,2,3,4]

result = string1[slice_object]

print(result)
hello

例子2 开始和结束位置

string1 = 'hello-world'

start = 2

stop=5

slice_object = slice(start, stop)

result = string1[slice_object]

print(result)
llo

例子3 开始,结束和步进

string1 = 'hello-world'

start = 2

stop = 9

step = 2

slice_object = slice(start, stop, step)

result = string1[slice_object]

print(result)
lowr

遍历

例子1 遍历

str = 'Hello! I am Robot. This is a Python example.'

splits = str.split()

for split in splits:

        print(split)

Python - Iterate Over Words of String

例子2 清空并遍历

import re

str = 'Hello! I am Robot. This is a Python example.'

#clean string

pat = re.compile(r'[^a-zA-Z ]+')

str = re.sub(pat, '', str).lower()

splits = str.split()

for split in splits:

        print(split)

Python - Iterate Over Words of String

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

relis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值