你們好,我又回來了! 以前的幾期咱們簡單瞭解了pandas的基礎操做,可是隻要涉及到數據,最多見的就是String(字符串)類型,所以不少時候咱們其實都在和字符串打交道,因此今天,我會把有關字符串的經常使用方法分享,但願可以幫到各位小夥伴~python
latitude = '37.24N'
longitude = '-115.81W'
'Coordinates {0},{1}'.format(latitude,longitude)
>>> 'Coordinates 37.24N,-115.81W'
複製代碼
f'Coordinates {latitude},{longitude}'
>>>'Coordinates 37.24N,-115.81W'
複製代碼
'{0},{1},{2}'.format(*('abc'))
>>>'a,b,c'
複製代碼
coord = {"latitude":latitude,"longitude":longitude}
'Coordinates {latitude},{longitude}'.format(**coord)
>>>'Coordinates 37.24N,-115.81W'
複製代碼
class Point:
def __init__(self,x,y):
self.x,self.y = x,y
def __str__(self):
return 'Point({self.x},{self.y})'.format(self = self)
def __repr__(self):
return f'Point({self.x},{self.y})'
複製代碼
test_point = Point(4,2)
test_point
>>> Point(4,2)
複製代碼
str(Point(4,2))
>>>'Point(4,2)'
複製代碼
" repr() shows the quote {!r}, while str() doesn't:{!s} ".format('a1','a2')
>>> " repr() shows the quote 'a1', while str() doesn't:a2 "
複製代碼
'{:<30}'.format('left aligned')
>>>'left aligned '
複製代碼
'{:>30}'.format('right aligned')
>>>' right aligned'
複製代碼
'{:^30}'.format('centerd')
>>>' centerd '
複製代碼
'{:*^30}'.format('centerd')
>>>'***********centerd************'
複製代碼
"int:{0:d}, hex:{0:x}, oct:{0:o}, bin:{0:b}".format(42)
>>>'int:42, hex:2a, oct:52, bin:101010'
複製代碼
'{:,}'.format(12345677)
>>>'12,345,677'
複製代碼
points = 19
total = 22
'Correct answers: {:.2%}'.format(points/total)
>>>'Correct answers: 86.36%'
複製代碼
import datetime as dt
f"{dt.datetime.now():%Y-%m-%d}"
>>>'2019-03-27'
複製代碼
f"{dt.datetime.now():%d_%m_%Y}"
>>>'27_03_2019'
複製代碼
today = dt.datetime.today().strftime("%d_%m_%Y")
today
複製代碼
'27_03_2019'
複製代碼
"this is a test".split()
>>>['this', 'is', 'a', 'test']
複製代碼
'do'*2
>>>'dodo'
複製代碼
orig_string ='Hello'
orig_string+',World'
>>>'Hello,World'
複製代碼
full_sentence = orig_string+',World'
full_sentence
>>>'Hello,World'
複製代碼
strings = ['do','re','mi']
', '.join(strings)
>>>'do, re, mi'
複製代碼
'z' not in 'abc'
>>> True
複製代碼
ord('a'), ord('#')
>>> (97, 35)
複製代碼
chr(97)
>>>'a'
複製代碼
s = "foodbar"
s[2:5]
>>>'odb'
複製代碼
s[:4] + s[4:]
>>>'foodbar'
複製代碼
s[:4] + s[4:] == s
>>>True
複製代碼
t=s[:]
id(s)
>>>1547542895336
複製代碼
id(t)
>>>1547542895336
複製代碼
s is t
>>>True
複製代碼
s[0:6:2]
>>>'fob'
複製代碼
s[5:0:-2]
>>>'ado'
複製代碼
s = 'tomorrow is monday'
reverse_s = s[::-1]
reverse_s
>>>'yadnom si worromot'
複製代碼
s.capitalize()
>>>'Tomorrow is monday'
複製代碼
s.upper()
>>>'TOMORROW IS MONDAY'
複製代碼
s.title()
>>>'Tomorrow Is Monday'
複製代碼
s.count('o')
>>> 4
複製代碼
"foobar".startswith('foo')
>>>True
複製代碼
"foobar".endswith('ar')
>>>True
複製代碼
"foobar".endswith('oob',0,4)
>>>True
複製代碼
"foobar".endswith('oob',2,4)
>>>False
複製代碼
"My name is yo, I work at SG".find('yo')
>>>11
複製代碼
# If can't find the string, return -1
"My name is ya, I work at Gener".find('gent')
>>>-1
複製代碼
# Check a string if consists of alphanumeric characters
"abc123".isalnum()
>>>True
複製代碼
"abc%123".isalnum()
>>>False
複製代碼
"abcABC".isalpha()
>>>True
複製代碼
"abcABC1".isalpha()
>>>False
複製代碼
'123'.isdigit()
>>>True
複製代碼
'123abc'.isdigit()
>>>False
複製代碼
'abc'.islower()
>>>True
複製代碼
"This Is A Title".istitle()
>>>True
複製代碼
"This is a title".istitle()
>>>False
複製代碼
'ABC'.isupper()
>>>True
複製代碼
'ABC1%'.isupper()
>>>True
複製代碼
'foo'.center(10)
>>>' foo '
複製代碼
' foo bar baz '.strip()
>>>'foo bar baz'
複製代碼
' foo bar baz '.lstrip()
>>>'foo bar baz '
複製代碼
' foo bar baz '.rstrip()
>>>' foo bar baz'
複製代碼
"foo abc foo def fo ljk ".replace('foo','yao')
>>>'yao abc yao def fo ljk '
複製代碼
'www.realpython.com'.strip('w.moc')
>>>'realpython'
複製代碼
'www.realpython.com'.strip('w.com')
>>>'realpython'
複製代碼
'www.realpython.com'.strip('w.ncom')
>>>'realpyth'
複製代碼
', '.join(['foo','bar','baz','qux'])
>>>'foo, bar, baz, qux'
複製代碼
list('corge')
>>>['c', 'o', 'r', 'g', 'e']
複製代碼
':'.join('corge')
>>>'c:o:r:g:e'
複製代碼
'www.foo'.partition('.')
>>>('www', '.', 'foo')
複製代碼
'foo@@bar@@baz'.partition('@@')
>>>('foo', '@@', 'bar@@baz')
複製代碼
'foo@@bar@@baz'.rpartition('@@')
>>>('foo@@bar', '@@', 'baz')
複製代碼
'foo.bar'.partition('@@')
>>>('foo.bar', '', '')
複製代碼
# By default , rsplit split a string with white space
'foo bar adf yao'.rsplit()
>>>['foo', 'bar', 'adf', 'yao']
複製代碼
'foo.bar.adf.ert'.split('.')
>>>['foo', 'bar', 'adf', 'ert']
複製代碼
'foo\nbar\nadfa\nlko'.splitlines()
>>>['foo', 'bar', 'adfa', 'lko']
複製代碼
除了我以上總結的這些,還有太多很是實用的方法,你們能夠根據本身的需求去搜索啦!git
我把這一期的ipynb文件和py文件放到了Github上,你們若是想要下載能夠點擊下面的連接:github