close
字首大寫

mystring = 'ALEX'
mystring += ' hello'

vralex = mystring.lower()[2:-1]

print vralex.title()

# Result:

Ex Hell

 

把字元放在中間 (共要多少字元,其它用什麼補)

mystring = 'ALEX'
mystring += ' hello'

vralex = mystring.lower()[2:-1]

print vralex.center(15, '#')

# Result:

####ex hell####

 

 

 

計算字元

mystring = 'ALEX'
mystring += ' hello'

vralex = mystring.lower()[2:-1]

print vralex.count('l')

# Result:

2

 

 

 

替換字元

mystring = 'ALEX'
mystring += ' hello'

vralex = mystring.lower()[2:-1]

print vralex.replace('l', 'y')

# Result:

ex heyy

 

 

 

 

加入字元

mystring = ['to' , 'day' , 'is' , 'friday']
print ' '.join(mystring)

# Result:

to day is friday

 

另一個寫法

mystring = ['to' , 'day' , 'is' , 'friday']
vralex= ' '.join(mystring)

print vralex

# Result:

to day is friday

 

 

添加字元

mystring = ['to' , 'day' , 'is' , 'friday']
mystring.append('alex')

mystring.append('so')

mystring.append('cool')

print mystring

# Result:

['to', 'day', 'is', 'friday', 'alex', 'so', 'cool']

 

插入字元

mystring = ['to' , 'day' , 'is' , 'friday']
mystring.append('alex')

mystring.append('so')

mystring.append('cool')

mystring.insert(4, 'and')

print mystring

# Result:

['to', 'day', 'is', 'friday', 'and', 'alex', 'so', 'cool']

 

 

顯示索引

mystring = ['to' , 'day' , 'is' , 'friday']
mystring.append('alex')
mystring.append('so')

mystring.append('cool')

mystring.insert(4, 'and')

print mystring.index('and')

# Result:

4

 

 

移除字元

mystring = ['to' , 'day' , 'is' , 'friday']

mystring.append('alex')

mystring.append('so')

mystring.append('cool')

mystring.insert(4, 'and')

mystring.pop(5)

print mystring

# Result:

['to', 'day', 'is', 'friday', 'and', 'so', 'cool']

 

另一種寫法

mystring = ['to' , 'day' , 'is' , 'friday']
mystring.append('alex')

mystring.append('so')

mystring.append('cool')

mystring.insert(4, 'and')

mystring.pop(mystring.index('so'))

print mystring

# Result:

['to', 'day', 'is', 'friday', 'and', 'alex', 'cool']

 

 

 

反轉順序

mystring = ['to' , 'day' , 'is' , 'friday']
mystring.append('alex')

mystring.append('so')

mystring.append('cool')

mystring.insert(4, 'and')

mystring.pop(mystring.index('so'))

mystring.reverse()

print mystring

# Result:

['cool', 'alex', 'and', 'friday', 'is', 'day', 'to']

 

 

 

 

由小到大排列

varalex = [10, -2, 4, 6]

varalex.sort()

print varalex

# Result:

[-2, 4, 6, 10]

 

由大到小排列

varalex = [10, -2, 4, 6]

varalex.sort(reverse=True)

print varalex

# Result:

[10, 6, 4, -2]

arrow
arrow
    文章標籤
    python
    全站熱搜

    A咖來D賽 發表在 痞客邦 留言(0) 人氣()