line_1 =
"你好"
line_2 =
" world"
line = line_1 + line_2
print
(line)
你好 world
line =
'123'
print
(line*
3)
123123123
line =
'123'
print
(len
(line)
)
3
line =
23print(id
(line)
)line_copy = line
line =
32print(id
(line)
)print(id
(line_copy)
)
140711792531808
140711792532096
140711792531808
str
="1234567890qwertyuioplkjhgfds"
#取開始到第10個的字串
print
(str[:
10])#取從第3個到第20個字元,每隔兩個取一次
print
(str[3
:20:2
])#取後十個字元
print
(str[-
10:])
#翻轉字元
print
(str[:
:-1]
)#取某乙個字元
print
(str[-
1])
1234567890
4680wryip
oplkjhgfds
sdfghjklpoiuytrewq0987654321
s
str
='hello world'
print
(str
.capitalize())
str=
'hello world'
print
(str
.capitalize(
))
hello world
hello world
str
='hello'
print
(str
.center(20,
'%')
)
%%%
%%%%hello%%%
%%%%
%
str
='123456789'
print
(str
.startswith(
'12'))
print
(str
.endswith(
'789'
))
true
true
str
='heeello'
print
(str
.count(
'e')
)
3
str
='123456789'
print
(str
.find(
'5')
)#在字串中查詢字元5,從第三個字元開始查詢
str.find(
'5',
3)
4
print
(str
.index(
'5')
)
str
='hello'
print
(str
.istitle(
))
true
str
=" hello world \n\t"
print
(str
.strip())
#只去除左端的
print
(str
.lstrip())
#只去除右端的
print
(str
.rstrip(
))
hello world
hello world
hello world
str
='aaddddkkkccc'
print
(str
.swapcase(
))
aaddddkkkccc
aaa Python字串操作
1 複製字串 str2 str1 2 鏈結字串 str abc 3 查詢字串 string.find sub string.index sub string.rfind sub string,rindex sub 4 字串比較 cmp str1,str2 cmp str1.upper str2.up...
Python字串操作
python如何判斷乙個字串只包含數字字元 python 字串比較 下面列出了常用的python實現的字串操作 strcpy sstr1,sstr2 sstr1 strcpy sstr2 sstr1 sstr1 strcpy2 print sstr2 strcat sstr1,sstr2 sstr1...
python字串操作
在 python 有各種各樣的string操作函式。在歷史上string類在 python 中經歷了一段輪迴的歷史。在最開始的時候,python 有乙個專門的string的module,要使用string的方法要先import,但後來由於眾多的 python 使用者的建議,從 python 2.0開...