Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What is a string in Python and how do you create one?

Python
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 27, 2026
5 min read

A Python string is an immutable sequence of Unicode characters, stored in the built-in str type. You create a Python string by putting text inside single quotes, double quotes or triple quotes, or by calling str() on another object. Strings are used for names, messages, file contents, URLs and almost every other piece of text in a program.

This guide is for beginners and uses Python 3.13 and 3.14. Every example can be pasted into a file or the interactive interpreter.

Table of Contents

  • What a string is
  • How to create a string in Python
    • Escape sequences and raw strings
  • Reading characters: indexing and slicing
  • Strings cannot be changed in place
  • Common string operations
  • Formatting strings with f-strings
  • Frequently Asked Questions
    • What is the type of a string in Python?
    • How do I convert a number to a string and back?
    • How do I check whether a string is empty?
    • Are Python strings Unicode?
  • Summary

What a string is

A string is an ordered collection of characters. "Ordered" means each character has a position, and "Unicode" means a string can hold letters from any language, digits, punctuation and emoji.

python
text

Here greeting is a name bound to a str object, type() shows the type and len() counts the characters (including the comma and the space).

How to create a string in Python

There are four common ways to create a string.

python

Single and double quotes work the same way. Pick one style and use the other when the text itself contains a quote: "It's fine" or 'He said "hi"'. Triple quotes (''' or """) let the text continue across lines.

Escape sequences and raw strings

A backslash starts an escape sequence inside a normal string.

SequenceMeaning
\nNew line
\tTab
\\A single backslash
\' and \"A quote character
\u00e9The Unicode character with that code point (here e with an acute accent)

Put an r before the opening quote to make a raw string, where backslashes are kept literally. Raw strings are handy for Windows paths and regular expressions.

python
text

Reading characters: indexing and slicing

Positions start at 0, and negative numbers count from the end.

python

Asking for an index that does not exist, such as word[10], raises IndexError. Slices never raise that error; they just return a shorter string.

Strings cannot be changed in place

A string is immutable: you cannot assign to one of its characters.

python

The last two lines build a new string and rebind the name. Every string method follows the same rule: it returns a new string and leaves the original alone.

Common string operations

python

Join and concatenate with care: "a" + "b" works, but "age: " + 30 raises TypeError because Python does not convert numbers automatically. Use an f-string instead.

Formatting strings with f-strings

F-strings (formatted string literals) are the standard way to insert values into text. Put f before the quote and write expressions inside curly braces.

python

The part after the colon is a format specifier: .1f means one decimal place, and , adds thousands separators. The {x = } form prints the expression and its value, which is useful for debugging. Since Python 3.12 you can reuse the same quote type inside an f-string expression and span multiple lines inside the braces.

Frequently Asked Questions

What is the type of a string in Python?

It is str. Python 3 has no separate character type; a single character is a string of length 1. Raw binary data uses bytes, not str.

How do I convert a number to a string and back?

Use str(42) to get "42", then int("42") or float("3.5") to convert back. Invalid text such as int("abc") raises ValueError.

How do I check whether a string is empty?

Use if not text:. An empty string is falsy, so this also works for the common case. Use text == "" when you want to be explicit.

Are Python strings Unicode?

Yes. Since Python 3, all str objects are Unicode, and source files are read as UTF-8 by default.

Summary

A Python string is an immutable str sequence of Unicode characters created with quotes, triple quotes or str(). You can index and slice it, call methods that return new strings, and build text with f-strings. Remember that strings never change in place, and that you must convert numbers explicitly when joining them with +.

Share

Similar posts

  • How do you format numbers with f-strings in Python?
  • How do you use f-strings in Python?
  • What are escape sequences in Python strings?
  • Why are strings immutable in Python?
  • How do you check if a string is a number in Python?
  • How do you count occurrences of a substring in Python?
  • How do you check if a string contains a substring in Python?
  • How do you change the case of a string in Python?