If the string contains single quotes, you can avoid the error by enclosing the string in double quotes.
spam = "That is Alice's cat"
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 .
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.
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
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!')
Indexes and slices can be applied to strings as well as lists.
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
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
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
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']
--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 '
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
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!'
This is the only basic review of Python ~ 4 ~
From the beginning This is the only basic review of Python ~ 1 ~
Recommended Posts