0% found this document useful (0 votes)
8 views3 pages

Python String Basics and Usage

A Python string is an immutable sequence of characters used for storing and manipulating textual data, created using single, double, or triple quotes. Triple quotes allow for multiline strings and preserve formatting, while strings can be treated like arrays for character access and manipulation. Python strings support various built-in methods for efficient text processing.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Python String Basics and Usage

A Python string is an immutable sequence of characters used for storing and manipulating textual data, created using single, double, or triple quotes. Triple quotes allow for multiline strings and preserve formatting, while strings can be treated like arrays for character access and manipulation. Python strings support various built-in methods for efficient text processing.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON

Python String
Python

Introduction to Python Strings


A Python string is an immutable sequence of characters enclosed in single, double, or
triple quotes.
It is used to store and manipulate textual data.
Strings support indexing, slicing, and various built-in methods for formatting,
searching, and modifying text efficiently in programming and data processing tasks.

Creating Strings
You can create strings in Python by enclosing text within single or double quotes.
Both single and double quotes are interchangeable for creating strings, but they must
match at the beginning and end of the string.
Triple quotes are used for multiline strings

single_quoted = 'This is a single-quoted string.'


double_quoted = "This is a double-quoted string."

triple_quoted = '''This is a
multiline string.'''
Note: in the multiline string result, the line breaks are inserted at the same position as
in the code.

Assign String to a Variable

my_string = "Intensity Coding"


print(my_string)

Intensity Coding

Triple Quotes

Python's triple quotes allow defining multi-line strings, preserving all formatting,
including newlines (\n), tabs (\t), and other special characters. This makes them useful
for handling long texts, documentation, or formatted output.
It is works with Both Single (''') and Double (""") Quotes.
The raw string (r'') ensures backslashes () are treated literally, making it useful for file
paths or regex patterns.

# Using triple quotes to define a multi-line string


multi_line_str = """This is a long string that spans
multiple lines and includes special characters like
TAB (\t) and NEWLINEs (\n) which appear as they are."""
print(multi_line_str)

# Normal string with escape characters


print('C:\\nowhere') # Output: C:\nowhere

# Raw string to ignore escape sequences


print(r'C:\\nowhere') # Output: C:\\nowhere
This is a long string that spans
multiple lines and includes special characters like
TAB ( ) and NEWLINEs (
) which appear as they are.
C:\nowhere
C:\\nowhere

Strings as Arrays
In Python, strings can be treated like arrays or sequences of characters in many ways.
However, it's important to note that Python strings are immutable, meaning you cannot
modify them directly.
Strings in Python are arrays of bytes representing unicode characters and It does not
have a character data type, a single character is simply a string with a length of 1.
You can access individual characters in a string using indexing and perform various
operations on them as if they were elements of an array. Here's how you can treat a
string as an array in Python.

You might also like