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)
#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)       
#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)

Enter fullscreen mode Exit fullscreen mode

原文链接:String Functions Examples

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
No matter when you start, it is important not to stop after the start.
无论你在什么时候开始,重要的是开始之后就不要停止
评论 抢沙发

请登录后发表评论

    暂无评论内容