Page cover

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

Replace with %s , %r

Align

Replace with %x , %o

Percentage

Date

Split without parameters

Concatenate :

Check string type, slice,count,strip :

Convert to list

总结

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

Last updated

Was this helpful?