2018-01-08 字符串處理函數

str.capitalize()返回一個字符串,首字母大寫

str.replace()替代字符

str.split()將字符串切分,返回一個列表,列表的元素是字符,默認用空格切分(把字符串切成列表)

str.join()參數是個可迭代的對象,返回的是一個字符串。

string 模塊

string.capitalize()

string.replace('hello','o','O')

string 


# from string import capitalize
#
# help(capitalize
s='hello'
print s.capitalize()
print s
# help (s.replace)
s='hello,h'
print s.replace('h','H')
print s.replace('h','H',1)

# help(str.replace)
print s.split()

s='hello a\tb\nc'
print s.split()

s1='abc'
print s1.split()
print s.split(' ')
ip='192.168.1.1'
print ip.split()
print ip.split('.')
print ip.split('.',1)

print range (10)
print ''.join([str(i) for i in range(10)])



Hello
hello
Hello,H
Hello,h
['hello,h']
['hello', 'a', 'b', 'c']
['abc']
['hello', 'a\tb\nc']
['192.168.1.1']
['192', '168', '1', '1']
['192', '168.1.1']
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

0123456789

print range (10)
print ''.join([str(i) for i in range(10)])
print int (''.join([str(i) for i in range(10)]))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0123456789
123456789

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章