[PYTHON] raw string

raw string

You may want to define the backslash itself as a string, such as a file path delimiter used in a Windows environment. Backslashes in string literals are treated as escape sequences, so you need to overlap them like "\". It's a hassle and makes string literals hard to see. If you want to treat characters as literals, including backslashes, it is convenient to use raw strings. "Raw" is an adjective meaning "raw". Raw strings define a string that is exactly what you typed. Escape sequences are not converted to control codes, etc., but are treated as character strings as they are. To define a raw string, prefix the quotes with an "r". For example, to define a Windows path as a raw string Do something like "r" C: \ path \ to \ file "".

[Use raw character strings] raw = r”C:¥path¥to¥file” raw ↓ ‘C:¥¥path¥¥to¥¥file’

Methods available for strings In the following, "S" is a character string object, and the argument in [] is optional. ※Optional

[Find () method: Search for a character string] S.find (string you want to search) [, start index [, end index]])

Searches for the "character string you want to search" from the beginning of the character string S, and returns the first found position with an index starting from 0. If not found, "-1" is returned. You can give an optional argument to specify the range to search. find () searches from the beginning of the string, but if you use a method called rfind (), it will search from the end (right) of the string.

[Index () method: Search for a character string] S.index (character string you want to search [, start index] [, end index])

It works the same as find (), but raises the exception "ValueError" if the "string you want to find" is not found. You can search for a string from the end (right) by using a method called rindex (), which is equivalent to rfind ().

[Endswith () method: Check the last string] S.endswith (string you want to search [, start index] [, end index])

Returns True if the string S ends with "the string you want to search for", false otherwise. You can specify the range to search by the argument given as an option.

[Starts with () method: Check the first string] S.startswith (string you want to search [, start index] [, end index])

Returns True if the string S starts with "the string you want to find", false otherwise. You can specify the range to search by the argument given as an option.

[Split () method: split the character string] S.split ([delimiter string [, number of splits]])

The character string S is separated by "delimiter character string", and a list of character strings is created and returned. Removes the delimiter string from the list string. If you do not specify the number of optional divisions, the division is performed up to the end of the character string. If specified, you can limit the number of splits. The number of splits in split () is counted from the beginning of the string, but you can specify the number of splits from the end (right) by using a method called rsplit ().

[Join () method: join character strings] S.join (sequence)

Concatenate the elements (strings) in the sequence using the string S. As a result, it returns a concatenated string (copy).

[Strip () method: delete the character string] S.strip ([string to delete])

Deletes the string from the beginning and end of the string. As a result, it returns the deleted string (copy). If no argument is specified, whitespace characters including spaces, tabs, etc. are removed. If you specify an argument, "character string to be deleted" Delete the target. The lstrip () method that performs the same processing only for the beginning (left) of the character string, and only the end There is also an rstrip () method.

[Upper () method: Convert alphabet to uppercase] S.upper()

Converts the lowercase letters of the string S to uppercase and returns a copy.

[Lower () method: Convert alphabet to lowercase] S.lower()

Converts the uppercase letters of the string S to lowercase and returns a copy.

[Ljust () method: Match the width of characters] S.ljust (width [, burying characters])

The character string S is "left-justified" in consideration of the width (numerical value). It is used to match the width when displaying a character string. If the length of the string is less than the width, fill it with spaces and copy and return the resulting string. In the argument of the option You can specify the character string to be used as the filling grass when adjusting the width. Similarly, there are rjust () methods for right justification and center () methods for centering.

Recommended Posts

raw string
String format
String format 2
String summary 1
Python string
Python: String concatenation
Python string format
python string slice
USB Raw Gadget
#Random string generation
Documentation string reconsidered
Character range / character string range
String search algorithm
Examine raw SQL
Python2 string type
Python string format
Python # string type
Remove'some character'from'some string'.
Python string inversion