Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
Python Strings Tutorial
Strings are a sequence of characters and are one of the most commonly used data types in Python. In this tutorial, we'll cover creating strings, accessing characters, basic string operations, and common string methods.
You can create a string by enclosing characters in single quotes ('
) or double quotes ("
).
string1 = 'Hello, World!' string2 = "Hello, World!"
For multiline strings, use triple quotes ('''
or """
).
multiline_string = '''Hello, World! This is a multiline string.'''
You can access individual characters in a string using indexing and slicing.
[]
with the index of the character you want to access.text = "Hello, World!" first_char = text[0] print(first_char) # Output: H
[]
with a start and end index separated by a colon :
to access a substring.text = "Hello, World!" substring = text[0:5] print(substring) # Output: Hello
+
operator to join two strings.string1 = "Hello" string2 = "World" result = string1 + ", " + string2 + "!" print(result) # Output: Hello, World!
*
operator to repeat a string a specified number of times.text = "Hello! " result = text * 3 print(result) # Output: Hello! Hello! Hello!
len()
function to find the length of a string.text = "Hello, World!" length = len(text) print(length) # Output: 13
lower()
: Converts a string to lowercase.text = "Hello, World!" lowercase_text = text.lower() print(lowercase_text) # Output: hello, world!
upper()
: Converts a string to uppercase.text = "Hello, World!" uppercase_text = text.upper() print(uppercase_text) # Output: HELLO, WORLD!
replace(old, new)
: Replaces all occurrences of the old substring with the new substring.text = "Hello, World!" replaced_text = text.replace("World", "Python") print(replaced_text) # Output: Hello, Python!
strip()
: Removes leading and trailing whitespace characters (spaces, tabs, and newlines) from a string.text = " Hello, World! " stripped_text = text.strip() print(stripped_text) # Output: Hello, World!
split(separator)
: Splits a string into a list of substrings based on the specified separator.text = "Hello, World!" split_text = text.split(", ") print(split_text) # Output: ['Hello', 'World!']
join(iterable)
: Joins elements of an iterable (e.g., list or tuple) into a single string using the string as a separator.words = ["Hello", "World!"] text = ", ".join(words) print(text) # Output: Hello, World!
Creating and manipulating strings in Python:
string1 = "Hello" string2 = 'World' concatenated_string = string1 + " " + string2
String concatenation in Python:
string1 = "Hello" string2 = 'World' concatenated_string = string1 + " " + string2
String indexing and slicing in Python:
original_string = "Python" first_char = original_string[0] substring = original_string[1:4]
Python string methods and functions:
original_string = "Python is amazing" length = len(original_string) uppercase_string = original_string.upper()
String formatting in Python:
.format()
method, or f-strings for concise and readable output.name = "Alice" age = 30 formatted_string = f"My name is {name} and I am {age} years old."
Escape sequences in Python strings:
\n
, \t
, etc.) to represent special characters within strings.special_string = "Tabbed \t text and new line \n Next line"
Checking string length in Python:
len()
function.original_string = "Python" length = len(original_string)
String comparison and equality in Python:
==
, !=
, <
, >
, etc.string1 = "Hello" string2 = 'Hello' is_equal = (string1 == string2)
Common string operations and tasks in Python:
original_string = "Python is versatile" substring_exists = "is" in original_string replaced_string = original_string.replace("versatile", "powerful")