This is the only basic review of Python ~ 3 ~

6. String manipulation

Single quote, double quote

If the string contains single quotes, you can avoid the error by enclosing the string in double quotes.

spam = "That is Alice's cat"

Escape character

However, if you want to use both single and double quotes, you can avoid it by using the escape character.

Escape character meaning
' Single quote
" Double quote
\t tab
\n new line
\|backslash
print("Hello there!\nHow are you?\nI\'m doing fine\t."
Hello there!
How are you?
I'm doing fine	.

In windows, \ becomes .

raw string

Preceding a quote character with r ignores the escape character in the string.

print(r'That is Carol\'s cat.')
That is Carol\'s cat.

Triple quotes

Using triple quotes eliminates the need to use escape characters. Python indentation rules also do not apply.

print('''Dear Alice,

Eve's cat has been arrested for catnapping, cat burglary, and extortion.

Sincerely,
Bob''')

Dear Alice,

Eve's cat has been arrested for catnapping, cat burglary, and extortion.

Sincerely,
Bob

Multi-line comment

Comments can be inserted from the place where the # symbol is attached to the end of the sentence. When you want to put a comment over multiple lines, enclose it in "" "as shown below.

def spam():
    """This is spam()To explain the behavior of the function,
This is a multi-line comment."""
    print('Hell!')  

String index and slice

Indexes and slices can be applied to strings as well as lists.

Convenient string method

Returns all uppercase strings.

spam = 'Hello world'
sapam = spam.upper()
spam
'HELLO WORLD'

Returns all strings in lowercase.

spam = 'Hello world'
sapam = spam.lower()
spam
'hello world'

Returns True if all letters of one or more letters are uppercase.

HELLO.isupper()
True

Returns True if all letters of one or more letters are lowercase.

hello.islower()
True

The string method isX

Returns True if it consists of only one or more alphabetic characters.

'hello'.isalpha()
True

Returns True if there are only one or more letters and numbers.

'hello123'.isalnum()
True

Returns True if it consists of only one or more numbers.

'123'.isdecimal()
True

Returns True if it consists only of spaces, tabs or newlines.

' '.isspace()
True

Returns True if it starts with an uppercase letter and all remaining letters are composed of lowercase letters.

Apple.istitle()
True

startswith () method, endswith () method

Returns True if a symmetric string begins or ends with the string passed to the method.

'Hello world!'.startswith('Hello')
True
'Hello world!'.endswith('World!')
True

join () method, split () method

--join () method

Concatenate a list of strings into a single string.

','.join(['cats','rats','bats'])
'cats,rats,bats'
'ABC'.join(['cats','rats','bats'])
'catsABCratsABCbats'

--split () method

Separate the character strings with the specified characters to make a list.

'My name is Simon'.split()
['My','name','is','Simon']
'MyABCnameABCisABCSimon'.split(ABC)
['My','name','is','Simon']

Align the text

--rjust () method --ljust () method

When the character string is larger than the specified value, the specified character is added to the right or left by that amount.

'Hello'.rjust(10, '*')
'*****Hello'
'Hello'.ljust(10, '*')
'Hello*****'

--center () method

When the character string is larger than the specified value, the specified character is added to the left and right by that amount.

'Hello'.center(10, '*')
'**Hello***'
'Hello'.center(10)
'  Hello   '

strip () method, rstrip () method, lstrip () method

Used to erase specified characters from the left, right, and both ends of a character string.

spam = '     Hello world  '
spam.strip()
'Hello world'
spam.lstrip()
'Hello world  '
spam.rstrip()
'     Hello world'

You can also erase the characters as shown below by passing an argument. The alphabetical order of the arguments is arbitrary, so it is as follows.

spam = 'SpamSpamBaconSpamEggsSpamSpam'
spam.strip(ampS)
BaconSpamEggs

pyperclip module

The pyperclip module has copy () and paste () functions. You can copy or paste it to your computer's clipboard.

import pyperclip
pyperclip.copy('Hello world!')
pyperclip.paste()
'Hello world!'

Continued

This is the only basic review of Python ~ 4 ~

From the beginning This is the only basic review of Python ~ 1 ~

Recommended Posts

This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
This is the only basic review of Python ~ 3 ~
Review of the basics of Python (FizzBuzz)
[python] [meta] Is the type of python a type?
Python Basic Course (at the end of 15)
The answer of "1/2" is different between python2 and 3
the zen of Python
Basic knowledge of Python
Is the Python 3 Engineer Certification Basic Exam Really Easy?
Summary of the basic flow of machine learning with Python
Impressions of taking the Python 3 Engineer Certification Basic Exam
Why is the first argument of [Python] Class self?
[Introduction to Python] Basic usage of the library matplotlib
Towards the retirement of Python2
About the ease of Python
Basic usage of Python f-string
About the features of Python
The Power of Pandas: Python
Python Basic Course (1 What is Python)
I wrote the basic grammar of Python with Jupyter Lab
What is the default TLS version of the python requests module?
Initial setting of Mac ~ Python (pyenv) installation is the fastest
The story of Python and the story of NaN
First Python 3 ~ The beginning of repetition ~
[Python] What is @? (About the decorator)
Existence from the viewpoint of Python
pyenv-change the python version of virtualenv
What kind of Kernel is this Kernel?
[python] What is the sorted key?
Change the Python version of Homebrew
About the basic type of Go
This and that of python properties
[Python] Understanding the potential_field_planning of Python Robotics
Basic grammar of Python3 system (dictionary)
What is the python underscore (_) for?
Science "Is Saito the representative of Saito?"
About the basics list of Python basics
Basic study of OpenCV with Python
Learn the basics of Python ① Beginners
A pharmaceutical company researcher summarized the basic description rules of Python
[Python] Organize the basic structure of Flask apps (Aim for de-copying)
March 14th is Pi Day. The story of calculating pi with python
[Python] Display only the elements of the list side by side [Vertical, horizontal]
How to use the asterisk (*) in Python. Maybe this is all? ..
A memorandum regarding the acquisition of the Python3 engineer certification basic exam
Change the length of Python csv strings
Check the behavior of destructor in Python
What is the XX file at the root of a popular Python project?
This and that of the inclusion notation.
[Linux] Review of frequently used basic commands 2
(Bad) practice of using this in Python
[Python3] Understand the basics of Beautiful Soup
Pass the path of the imported python module
The story of making Python an exe
Learning notes from the beginning of Python 1
Check the existence of the file with python
About the virtual environment of python version 3.7
What kind of programming language is Python?
Comparing the basic grammar of Python and Go in an easy-to-understand manner
Review the concept and terminology of regression