Microsoft Windows [版本 10.0.22000.856]
(c) Microsoft Corporation。保留所有权利。
C:\Users\komorip>python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> s='spam'
>>> s[-1]
'm'
>>> len(s)
4
>>> s[1:2]
'p'
>>> s[1:]
'pam'
>>> s[0:-1]
'spa'
>>> s[1:-2]
'p'
>>> s[:-1]
'spa'
>>> s[:]
'spam'
>>> x=s+'pam'
>>> x
'spampam'
>>> x*2
'spampamspampam'
>>> x
'spampam'
>>> s[0]=q
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'q' is not defined
>>> s[0]='q'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> s='z'+s[:]
>>> s
'zspam'
>>> L=list(s)
>>> L
['z', 's', 'p', 'a', 'm']
>>> L[0]='p'
>>> L
['p', 's', 'p', 'a', 'm']
>>> L=''.join(L)
>>> L
'pspam'
>>> L.find('pam')
2
>>> L.replace('p','h')
'hsham'
>>> L
'pspam'
>>> L.split('s')
['p', 'pam']
>>> L.split('p')
['', 's', 'am']
>>> L.upper()
'PSPAM'
>>> L.isalpha()
True
>>> L=L+'\n'
>>> L
'pspam\n'
>>> L[:-1]
'pspam'
>>> L.rstrip()
'pspam'
>>> L
'pspam\n'
>>> L.rstrip()
'pspam'
>>> L.rstrip()
'pspam'
>>> L.rstrip().upper().split('a')
['PSPAM']
>>> L.rstrip().upper().split('A')
['PSP', 'M']
>>> L=rstrip()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'rstrip' is not defined
>>> L
'pspam\n'
>>> '%s Hello'%('lisang')
'lisang Hello'
>>> '{} Hello'.format('lisang')
'lisang Hello'
>>> '%s Hello'%('lisang')
'lisang Hello'
>>> '{0}Hello'format('lisang')
File "<stdin>", line 1
'{0}Hello'format('lisang')
^
SyntaxError: invalid syntax
>>> '{0}Hello'.format('lisang')
'lisangHello'
>>>