首页 > 后端开发 > Python教程 > 字符串函数示例

字符串函数示例

Linda Hamilton
发布: 2025-01-06 23:59:52
原创
416 人浏览过

String Functions Examples

#1. L Strip
#2. R Strip
#3. Replace a string in a statement with another string

# L Strip
Str = input("Enter the String:")

ord_space = ord(' ')
ord_tab = ord('\t')

i = 0 
right = 0
while i < len(Str):
    if ord(Str[i]) != ord_space and ord(Str[i]) != ord_tab:
        right = i
        break
    i+=1
Str = Str[right:]
print('LStrip String:',Str,end='#',sep='')


# R Strip

Str = input("Enter the String:")

ord_space = ord(' ')
ord_tab = ord('\t')

i = len(Str)-1
right = 0 

while i > 0:
    if ord(Str[i]) != ord_space and ord(Str[i]) != ord_tab:
        right = i
        break
    else:
        i-=1

Str = Str[:right+1]

print("#", Str, "#", sep='')


# Replace a string

#MSD --> Dhoni
#MSD is Former Indian Captain.  MSD is retired. MSD plays for CSK
sen = 'MSD is Former Indian Captain.  MSD is retired. MSD plays for CSK - MSD'
Key = 'MSD'
New_sen = ''
Key_Len = len(Key)
i=0
while i< len(sen):
       if Key == sen[i:i+Key_Len]:
            print('i',i)
            New_sen = New_sen + 'Dhoni'
            i+=Key_Len
       else:
            New_sen = New_sen + sen[i]
            i+=1
print(New_sen)       
登录后复制

以上是字符串函数示例的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板