3

有关字符串基本方法

大家好,我又回来了! 之前的几期我们已经简单了解了pandas的基础操作,但是只要涉及到数据,最常见的就是String(字符串)类型,所以很多时候我们其实都在和字符串打交道,所以今天,我会把我自己总结的,有关字符串的常用方法分享给大家,希望能够帮到各位小伙伴~

Split and format

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'

Access argument' s attribute

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)'

Replace with %s , %r :

" repr() shows the quote {!r}, while str() doesn't:{!s} ".format('a1','a2')
>>> " repr() shows the quote 'a1', while str() doesn't:a2 "

Align :

'{:<30}'.format('left aligned')
>>>'left aligned                  '
'{:>30}'.format('right aligned')
>>>'                 right aligned'
'{:^30}'.format('centerd')
>>>'           centerd            '
'{:*^30}'.format('centerd')
>>>'***********centerd************'

Replace with %x , %o :

"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'

Percentage :

points = 19
total = 22
'Correct answers: {:.2%}'.format(points/total)
>>>'Correct answers: 86.36%'

Date :

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'


Split without parameters :

"this is a  test".split()
>>>['this', 'is', 'a', 'test']

Concatenate :

'do'*2
>>>'dodo'
orig_string ='Hello'
orig_string+',World'
>>>'Hello,World'
full_sentence = orig_string+',World'
full_sentence
>>>'Hello,World'

Check string type , slice,count,strip :

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'

Convert to lists :

', '.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']

总结

除了我以上总结的这些,还有太多非常实用的方法,大家可以根据自己的需求去搜索啦!

我把这一期的ipynb文件和py文件放到了Github上,大家如果想要下载可以点击下面的链接:

希望大家能够继续支持我,完结,撒花


alpha94511
549 声望996 粉丝

Python爱好者, 前端开发厌恶者