From the python docs for literals: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.

Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters. As a result, in string literals, '\U' and '\u' escapes in raw strings are not treated specially. Given that Python 2.x’s raw unicode literals behave differently than Python 3.x’s the 'ur' syntax is not supported.

and

A string literal with 'f' or 'F' in its prefix is a formatted string literal; see Formatted string literals. The 'f' may be combined with 'r', but not with 'b' or 'u', therefore raw formatted strings are possible, but formatted bytes literals are not.

So:

  • r means raw
  • b means bytes
  • u means unicode
  • f means format

The r and b were already available in Python 2, as such in many other languages (they are very handy sometimes).

Since the strings literals were not unicode in Python 2, the u-strings were created to offer support for internationalization. As of Python 3, u-strings are the default strings, so "..." is semantically the same as u"...".

Finally, from those, the f-string is the only one that isn't supported in Python 2.

Answer from Vitor SRG on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.7 documentation
The following table summarizes the text and binary sequence types methods by category. Textual data in Python is handled with str objects, or strings. Strings are immutable sequences of Unicode code points.
🌐
W3Schools
w3schools.com › python › python_strings.asp
Python Strings
Like many other popular programming languages, strings in Python are arrays of unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1.
🌐
Google
developers.google.com › google for education › python › python strings
Python Strings | Python Education | Google for Developers
Python does not have a separate character type. Instead an expression like s[8] returns a string-length-1 containing the character. With that string-length-1, the operators ==, <=, ...
🌐
Medium
medium.com › @muhammadshafey063 › python-strings-and-their-types-with-key-features-a181e2db01ee
Python: Strings and their types with key features | by Muhammad shafey | Medium
June 2, 2024 - Python: Strings and their types with key features In Python, a string is a sequence of characters enclosed in quotes (either single quotes ‘ or double quotes “ “). Strings are immutable …
Top answer
1 of 4
19

From the python docs for literals: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.

Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters. As a result, in string literals, '\U' and '\u' escapes in raw strings are not treated specially. Given that Python 2.x’s raw unicode literals behave differently than Python 3.x’s the 'ur' syntax is not supported.

and

A string literal with 'f' or 'F' in its prefix is a formatted string literal; see Formatted string literals. The 'f' may be combined with 'r', but not with 'b' or 'u', therefore raw formatted strings are possible, but formatted bytes literals are not.

So:

  • r means raw
  • b means bytes
  • u means unicode
  • f means format

The r and b were already available in Python 2, as such in many other languages (they are very handy sometimes).

Since the strings literals were not unicode in Python 2, the u-strings were created to offer support for internationalization. As of Python 3, u-strings are the default strings, so "..." is semantically the same as u"...".

Finally, from those, the f-string is the only one that isn't supported in Python 2.

2 of 4
16
  1. u-strings is for unicode in python 2. Most probably you should forget this, if you're working with modern applications — default strings are all unicode in python 3, and if you're migrating from python 2, you should most probably use from __future__ import unicode_literals, which makes [almost] the same for python 2

  2. b-strings is for raw bytes — they have no idea of text, rather just stream of bytes. Sometimes used as input for your source, most often as result of network or low-level code — reading data in binary format, unpacking archives, working with encryption libraries etc.

    Moving from/to b-string to str done via

    # python 3
    >>> 'hēllö'.encode('utf-8')
    b'h\xc4\x93ll\xc3\xb6'
    
    >>> b'h\xc4\x93ll\xc3\xb6'.decode()
    'hēllö'
    
    # python 2 without __future__
    >>> u'hēllö'.encode('utf-8')
    'h\xc4\x93ll\xc3\xb6'
    
    >>> 'h\xc4\x93ll\xc3\xb6'.decode('utf-8')
    u'h\u0113ll\xf6'  # this is correct representation
    
  3. r-strings are not specifically for regex, those are "raw" strings. Unlike regular string literals, r-string doesn't give any special meaning for escape characters. I.e. normal string 'abc\n' is 4 characters long, last char is "newline" special character. To provide it in literal, we're using escaping with \. For raw strings, r'abc\n' is 5-length string, last two characters are literally \ and n. Two places to see raw strings often are:

  • regex patterns — to not mess escaping with actual special characters in patters

  • file path notations for windows systems, as windows family uses \ as delimeter, normal string literals will look like 'C:\\dir\\file', or '\\\\share\\dir', while raw would be nicer: r'C:\dir\file' and r'\\share\dir' respectively

  1. One more notable is f-strings, which came to life with python 3.6 as simple and powerful way of formatting strings:
  • f'a equals {a} and b is {b}' will substitute variables a and b in runtime.
🌐
Real Python
realpython.com › ref › builtin-types › str
str | Python’s Built-in Data Types – Real Python
The built-in str data type represents sequences of characters and is used to manipulate textual data. Python strings are immutable sequences, meaning once defined, they can’t be changed.
🌐
Dataquest
dataquest.io › home › blog › python strings: an in-depth tutorial (55+ code examples)
Python Strings: An In-Depth Tutorial (55+ Code Examples)
April 24, 2025 - We use the type() function to determine the data type of an object in Python. It returns the class of the object. When the object is a string, it returns the str class. Similarly, it returns dict, int, float, tuple, bool class when the object is a dictionary, integer, float, tuple, or Boolean, respectively.
🌐
APXML
apxml.com › courses › python-for-beginners › chapter-2-python-basics-variables-data-types-operators › python-string-type
Python String Data Type | Working with Text
An important characteristic of Python strings is that they are immutable. This means that once a string is created, it cannot be changed in place. Operations that seem to modify a string, like concatenation or methods we'll see next, actually create and return new strings. greeting = "hello" # greeting[0] = "H" # This will cause a TypeError: 'str' object does not support item assignment # To "change" the greeting, create a new string new_greeting = "H" + greeting[1:] print(new_greeting) # Output: Hello print(greeting) # Output: hello (original string is unchanged)
Find elsewhere
🌐
Python
docs.python.org › 3 › library › string.html
Common string operations — Python 3.14.7 documentation
The default version understands ‘s’ (str), ‘r’ (repr) and ‘a’ (ascii) conversion types. The str.format() method and the Formatter class share the same syntax for format strings (although in the case of Formatter, subclasses can define their own format string syntax).
🌐
Readthedocs
portingguide.readthedocs.io › en › latest › strings.html
Strings — Conservative Python 3 Porting Guide 1.0 documentation
We recommend replacing the word “string” in developer documentation (including docstrings and comments) with either “text”/“text string” or “bytes”/“byte string”, as appropriate. Additionally, code that supports both Python 2 and 3 in the same codebase can use what is conceptually a third type:
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string
Python String - GeeksforGeeks
Strings are sequence of characters written inside quotes. It can include letters, numbers, symbols and spaces. Python does not have a separate character type.
Published: June 11, 2026
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-string.html
Python Strings
>>> s = 'Python' >>> len(s) 6 >>> len('') # empty string 0 · The formal name of the string type in Python is "str". The str() function serves to convert many values to a string form.
🌐
Medium
medium.com › @datasciencejourney100_83560 › all-about-strings-in-python-83772b9f2900
String Data Type In Python. In this blog, we will study about… | by Rina Mondal | Medium
September 3, 2025 - In this blog, we will study about strings in Python. String is a built in datatype which is a sequence of characters. #Create a string mystring="DataScience" print(mystring) #Here, DataScience is a string #String can contain a single or multiple characters. mystring1= 'I love Data Science' print(mystring) #To check the datatype of any variable, we can use the type function.
🌐
DEV Community
dev.to › atifwattoo › what-is-string-and-its-types-in-python-4ide
What is String and its types in Python? - DEV Community
February 5, 2025 - In Python, a string is a sequence of characters enclosed within single quotes ('), double quotes ("), or triple quotes (''' or """).
🌐
Full Stack Python
fullstackpython.com › blog › python-basic-data-types-strings.html
Basic Data Types in Python 3: Strings - Full Stack Python
October 18, 2019 - One of the most common data types in any programming language is a string. A string represents a series of characters, which you would use to represent usernames, blog posts, tweets, or any text content in your code. You can create a string and assign it to a variable like this. ... In Python, strings are considered immutable - once you create them, they can't be changed.
🌐
Plain English
python.plainenglish.io › 6-types-of-python-strings-i-wish-i-learned-earlier-ea2a1076483c
6 Types of Python Strings I Wish I Learned Earlier… | by Kiran Grewal | Python in Plain English
October 15, 2025 - Turns out, raw strings just ignore escape sequences. text = r"C:\new\folder" print(text) # C:\new\folder · No more messing around with double backslashes. Wish I knew this during my regex struggles. I used to get those weird errors with encodings. ... At first, I avoided them. Now I see why they matter when working with files, APIs, or sockets. This one confused me until I wrote code with emojis. Python 3 treats all strings as Unicode by default, but still — understanding it earlier…
🌐
Luis Llamas
luisllamas.es › home › courses › python programming course
Python string Type for Text Data
November 20, 2024 - In Python, strings str are immutable sequences of Unicode characters used to represent text.
🌐
Real Python
realpython.com › python-data-types
Basic Data Types in Python: A Quick Exploration – Real Python
May 22, 2026 - Python has several built-in data types that you can use out of the box because they’re built into the language. From all the built-in types available, you’ll find that a few of them represent basic objects, such as numbers, strings and characters, bytes, and Boolean values.
🌐
Towards Data Science
towardsdatascience.com › home › latest › python string data type explained
Python String Data Type Explained | Towards Data Science
January 26, 2025 - In Python, strings are immutable sequences of characters, and are used to handle textual data. Key things you should know about strings, they are: ... Learning data types in each programming language is essential to understand the code and programs.