Summary of string operations

I would like to summarize the string operations in various languages. We will add languages from time to time. Table of Contents

String concatenation

Python

str = str1 + str2
str = ','.join(list) #','Combine lists by specifying as a delimiter
str = str1*n #Repeat the same string n times

Value embedding

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.

Cut out a character string

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

String replacement

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)

Splitting strings

Python

list = str.split() #Split with whitespace
list = str.split(',') #,Split with

String search

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

Uppercase / lowercase

Python

str = str1.upper()
str = str1.lower()

Check if it is a number

Python

str.isdigit()

Recommended Posts

Summary of string operations
Summary of python file operations
Summary of Python3 list operations
String summary 1
Summary of various operations in Tensorflow
Summary of operations often performed with asyncpg
Numerical summary of data
Various character string operations
Summary of Tensorflow / Keras
Summary of pyenv usage
[Python] Summary of S3 file operations with boto3
Summary of Python arguments
[Python] Chapter 02-05 Basics of Python programs (string operations / methods)
Summary of logrotate software logrotate
Summary of test method
Summary of Excel operations using OpenPyXL in Python
I tried to summarize the string operations of Python
2017.3.6 ~ 3.12 Summary of what we did
Various Python built-in string operations
Convenient usage summary of Flask
Summary of Linux distribution types
Basic usage of Pandas Summary
A brief summary of Linux
Summary of Proxy connection settings
Summary of how to use pandas.DataFrame.loc
Summary of basic knowledge of PyPy Part 1
Summary of basic implementation by PyTorch
Summary of scraping relations (selenium, pyautogui)
H29.2.27 ~ 3.5 Summary of what I did
Summary of Stack Overflow Developer Survey 2020
Summary of how to use pyenv-virtualenv
[Shell] Various patterns of string decomposition
Machine learning ③ Summary of decision tree
Trivia: String representation of constant values
A rough summary of OS history
Automation of remote operations with Fabric
A brief summary of qubits (beginners)
Summary of go json conversion behavior
A Tour of Go Learning Summary
Summary of "nl command Advent Calendar 2020"
[Anaconda3] Summary of frequently used commands
Summary of how to use csvkit
[For competition professionals] Summary of doubling
Summary of Python indexes and slices
Summary of multi-process processing of script language
Summary of restrictions by file system
[OpenCV; Python] Summary of findcontours function
Summary of processes often performed in Pandas 1 (CSV, Excel file related operations)
Conversion of string <-> date (date, datetime) in Python
[Python] Summary of how to use pandas
2014/02/28 Summary of contents demoed at #ssmjp, part 1
Summary of Oracle Database XE installation procedure
Summary of frequently used commands of django (beginner)
Summary of methods often used in pandas
Summary of methods for automatically determining thresholds
Summary of gamma distribution parameter specification method
Summary of frequently used commands in matplotlib
[Linux] Summary of middleware version confirmation commands
(Java, JavaScript, Python) Comparison of string processing
Summary of study done this week (H29.2.13 ~ 2.24)
Omit BOM from the beginning of the string