Strings can begin and end with either single quote ' '
or double quotes " "
Escape characters \
let you put quotes and other characters that are hard to type inside strings
# 'That is Alice's cat' # SyntaxError
'That is Alice\'s cat'
Escape Character | Prints as |
---|---|
\' | Signle quote |
\" | Double quotes |
\t | Tab |
\n | Newline (line break) |
\ | Backslash |
Raw strings r' '
will literally print any backslash \
in the string and ignore escape characters
r'That is Carol\'s cat.'
print(r'That is Carol\'s cat.')
Multiline strings start and end with three quotes '''
or """
, and can span multiple lines.
print("""Dear Alice,
Eve's cat has been arrested for catnapping
Sincerely,
Bob.""")
spam = """Dear Alice,
Eve's cat has been arrested for catnapping
Sincerely,
Bob."""
spam
Indexes, slices, and the in
and not in
operators all work with strings
spam = 'Hello world!'
spam[0]
spam[1:5]
spam[-1]
'Hello' in spam
'HELLO' in spam
'x' in spam
Since strings are immutable, string methods return a new string value rather than modifying string in place
Recall strings are immutable
spam = 'Hello World'
spam.upper()
spam
The upper()
method returns an uppercase
spam = spam.upper()
spam
The lower()
method returns s lowercase string
spam.lower()
The title()
method returns a title case string.
'hello world'.title()
The isupper()
, islower()
methods return boolean value (True or False) if the string is that respective kind of string
spam = 'Hello World'
spam.islower()
spam = 'HELLO WORLD'
spam.isupper()
spam = ''
spam.isupper()
spam.islower()
The isalpha()
, isalum()
, isdecimal()
, isspace()
, istitle()
methods return boolean value (True or False) if the string is that respective kind of string
Method | Desc |
---|---|
isalpha() | letters only |
isalnum() | letters and numbers only |
isdecimal() | numbers only |
isspace() | whitespace only |
istitle() | titlecase only |
'hello'.isalpha()
'hello123'.isalpha()
'hello123'.isalnum()
'132'.isdecimal()
' '.isspace()
'Hello World'.isspace()
'Hello World'[5].isspace()
'This Is Title Case'.istitle()
The startswith()
, endswith()
methods return boolean values
'Hello world'.startswith('H')
'Hello world'.startswith('el')
'Hello world'.endswith('world')
'Hello world'.endswith('a')
The join
method returns a string that combines the strings in a list
','.join(['cats', 'rats', 'bats'])
' '.join(['cats', 'rats', 'bats'])
'\n\n'.join(['cats', 'rats', 'bats'])
print('\n\n'.join(['cats', 'rats', 'bats']))
The split()
method returns a list of strings, split from the string it's called on
'My name is Simon'.split()
'My name is Simon'.split('m')
The ljust()
, rjust()
, center()
methods return a string padded with spaces
'Hello'.rjust(10)
len(' Hello')
'Hello'.ljust(10) # 'Hello '
'Hello'.rjust(10, '*')
'Hello'.center(10)
'Hello'.center(10, '=')
The strip()
, rstrip()
, lstrip()
methods return a string with whitespace stripped off the sides
spam = 'Hello'.rjust(10)
spam
spam.strip()
' x '.strip()
' x '.rstrip()
' x '.lstrip()
'SpamSpamBaconSpamEggs'.strip('ampS')
The replace()
method will replace all occurrences of the first string argument with the second string argument
'Hello there'.replace('e', 'XYZ')
'Hello'.upper().isupper()
The pyperclip
module has copy()
and paste()
functions for using the clipboard
Check if pyperclip is installed
import pyperclip
import pyperclip
pyperclip.copy('Hello') # to clipboard
pyperclip.paste()
name = 'Alice'
place = 'Main Street'
time = '6 pm'
food = 'turnips'
print("""Hello %s, you are invite to s party at %s
at %s. Please bring %s.""" %(name, place, time, food))
name = 'Sam'
print('hello {}'.format(name))
name = 'John'
age = 12
print('Hi my name is {y} and my age is {x}'.format(x=age, y=name))