1. 程式人生 > 其它 >python字串的擷取與替換

python字串的擷取與替換

# encoding: utf-8
"""
@author: Sunny
@software: PyCharm
@file: demo01.py
@description: 字串的遍歷與替換
@time: 12/22/2021  4:35 PM
"""
a=1
print(isinstance(a,int))
print(isinstance(a,float))
str1='She is very cute.'
print(str1[:])#She is very cute.
print(str1[0:])#She is very cute.
print(str1[1:5])#he i
print(str1[-4:-1])#
ute print(str1[:-1])#She is very cute print(str1*3) str2='She is clever. She is cute. She is very beautiful.' print(str2.replace('She','Sunny')) print(str2.replace('She','Tianner',1)) print(str2[::-1].replace('ehS','ynnuS',1)[::-1]) print(str2.replace('She','Sunny',2).replace('Sunny','She',1))



執行結果如下:
C:\Users\administrator\Desktop\WebFrame\venv\Scripts\python.exe C:/Users/administrator/Desktop/WebFrame/basic_grammar/num_type/demo01.py
True
False
She is very cute.
She is very cute.
he i
ute
She is very cute
She is very cute.She is very cute.She is very cute.
Sunny is clever. Sunny is cute. Sunny is very beautiful.
Tianner 
is clever. She is cute. She is very beautiful. She is clever. She is cute. Sunny is very beautiful. She is clever. Sunny is cute. She is very beautiful.