I would like to summarize the string operations in various languages. We will add languages from time to time. Table of Contents
Python
str = str1 + str2
str = ','.join(list) #','Combine lists by specifying as a delimiter
str = str1*n #Repeat the same string n times
Python
'%s, %s!' % ('Hello', 'world') #'Hello, world! 'printf format(See section C)
'%(x)s, %(y)s!' % {'x':'Hello', 'y':'world'} #'Hello, world!'printf format, dictionary reference
format function
'{0}, {1}'.format('Hello','world') #'Hello, world'
'{0:30}'.format('aa') #Specify the number of digits(30)
'{0:<30}'.format('aa') #Left justified
'{0:>30}'.format('aa') #Right justified
'{0:^30}'.format('aa') #Centered
'{0:*<30}'.format('aa') #Fill character(*)Designation
'{:+}'.format(10) #Show sign
'{:-}'.format(-10) #Show only negative numbers
'{: }'.format(10) #Only negative numbers are displayed, if positive' 'Show
'{:.3}'.format(3.1415) #3.14 digits(3)Specify
'{:.3f}'.format(3.1415) #3.142 Number of digits after the decimal point(3)Specify
'{:,}'.format(5000000000000000) #3-digit comma separated
'{:.2%}'.format(30.0/113.1) #'26.53%'Percentage display
'10:{0:d},16:{0:x},8:{0:o},2:{0:b}'.format(12) #Specify a base number(X in hexadecimal:Lowercase, X:uppercase letter)
'{:%Y-%m-%d %H:%M:%S}'.format(date) #date
C
sprintf(s, "%s %s", "Hello", "world") //Hello,Substitute in world s(s:char[])
See the printf function section for details.
Python
str = 'ABCDEFGH'
str[1] #'B'2nd character
str[1:3] #'ABC'2nd to 3rd characters
str[3:] #'DEFGEH'4th and subsequent characters
str[:3] #'ABC'Up to the third character
str[-3:] #'FGH'3 letters from the right
Python
str = str1.replace(from, to)
str = str1.replace(from, to, count) #Replace by specifying the number
When using regular expressions
import re
pattern = re.compile('(r.*a)$')
str = pattern.sub('\1', sur1)
Python
list = str.split() #Split with whitespace
list = str.split(',') #,Split with
Python
str = 'ABCDEFABC'
str.find('BC') #1 Search from the front
str.rfind('BC') #7 Search from the back
str.find('KK') #-1 When it does not exist-Returns 1
str1 in str #Whether the string is included
str.count(str1) #Count the number of str1s contained in str
When using regular expressions
import re
pattern = re.compile('(r.*a)$')
m = pattern.search('\1', start) #start:Search start position(0 start)
m.start() #Returns the start position
m.end() #Returns the end position
Python
str = str1.upper()
str = str1.lower()
Python
str.isdigit()
Recommended Posts