大佬们,初学者想知道怎么可以在pycharm里这样将“chengdu"打印出来啊

c

ch

che

chen

cheng

chengd

chengdu

1.不使用循环

str="chengdu"
print(str[:1])
print(str[:2])
print(str[:3])
print(str[:4])
print(str[:5])
print(str[:6])
print(str[:7])

2.使用循环
str="chengdu"
for i in range(len(str)):
    i=i+1
    print(str[:i])

 

请学习一下 Python 的substring 的功能: 

 

>>> x = "Hello World!"
>>> x[2:]
'llo World!'
>>> x[:2]
'He'
>>> x[:-2]
'Hello Worl'
>>> x[-2:]
'd!'
>>> x[2:-2]
'llo Worl'