Python: Quote String
What is String
A string is a sequence of characters.
- In Python 3, it's a sequence of Unicode characters.
- In Python 2, it's a sequence of Bytes (representing ASCII Characters). But if prefixed with
u
, then it's a sequence of Unicode characters.
String is Immutable
Any function that work on string returns a new string.
String Syntax
there are multiple syntax for string.
example
"single quoted string with QUOTATION MARK delimiter"
'single quoted string with APOSTROPHE delimiter'
"""triple quoted string with QUOTATION MARK delimiter"""
'''triple quoted string with APOSTROPHE delimiter'''
Single Quoted String (For Single Line, Short String)
example
"QUOTATION MARK delimited"
'APOSTROPHE delimited'
String with different delimiters basically have the same meaning. Only difference is that QUOTATION MARK quoted cannot contain unescaped QUOTATION MARK, and APOSTROPHE quoted cannot contain unescaped APOSTROPHE. (escape means backslash character sequence. 〔see Python: String Escape Sequence〕)
in single quoted string:
- Literal newline character is not allowed. It's syntax error.
- Has special interpretation of backslash character (see String Escape Sequence), unless it's prefixed with
r
, known as Raw String (r-prefix)
xx = "this is string" yy = 'another string' print(xx, yy)
# Literal linebreak is not allowed xx = "abc xyz" # xx = "abc # ^ # SyntaxError: EOL while scanning string literal # error: cannot format -: Cannot parse: 1:3: a = "abc
Triple Quote String
No Here-String
Python does not suport Here-String (aka here-doc)
Python, String
- Python: Quote String
- Python: Triple Quote String
- Python: String Escape Sequence
- Python: Unicode Escape Sequence
- Python: String Prefix Character (u f r b)
- Python: Raw String (r-prefix)
- Python: f-String (Format, Template)
- Python: Print
- Python: Join String
- Python: Format String (Convert to String)
- Python: String Operations and Methods
- Python: Search Substring
- Python: Split String
- Python: String, Check Case, Char Class
- Python: Letter Case Conversion
- Python: Unicode 🐍
- Python 2: Unicode Tutorial