Python Strings

Python Strings

Section (1) – Introduction to Strings in Python

In Python, a string is a sequence of characters enclosed within single quotes (‘ ‘) or double quotes (” “). It is one of the built-in data types in Python and is widely used in various applications.

Creating a String

We can create a string by assigning a value enclosed within quotes to a variable. For example:

string_variable = "This is a string."

We can also use single quotes to create a string, as shown below:

string_variable = 'This is also a string.'

Accessing Characters in a String

We can access individual characters in a string using indexing. In Python, indexing starts at 0, so the first character in a string has an index of 0, the second character has an index of 1, and so on. For example:

string_variable = "Hello, World!"
print(string_variable[0])   # Output: H
print(string_variable[1])   # Output: e

We can also use negative indexing to access characters from the end of the string. For example:

string_variable = "Hello, World!"
print(string_variable[-1])  # Output: !
print(string_variable[-2])  # Output: d

String Slicing

We can extract a portion of a string using string slicing. String slicing allows us to create a new string from a portion of an existing string. The syntax for string slicing is string_variable[start_index:end_index]. For example:

string_variable = "Hello, World!"
print(string_variable[0:5])  # Output: Hello

If we omit the start_index, Python assumes it to be 0. If we omit the end_index, Python assumes it to be the end of the string. For example:

string_variable = "Hello, World!"
print(string_variable[:5])   # Output: Hello
print(string_variable[7:])   # Output: World!

String Concatenation

We can concatenate two or more strings using the + operator. For example:

string1 = "Hello"
string2 = "World"
string3 = string1 + " " + string2
print(string3)  # Output: Hello World

We can also repeat a string multiple times using the * operator. For example:

string_variable = "Hello"
print(string_variable * 3)  # Output: HelloHelloHello

String Methods

Python provides several built-in string methods that we can use to manipulate strings. Some of the commonly used string methods are:

  • len(string): Returns the length of the string.
  • string.lower(): Converts all the characters in the string to lowercase.
  • string.upper(): Converts all the characters in the string to uppercase.
  • string.replace(old, new): Replaces all occurrences of the old substring with the new substring.
  • string.split(separator): Splits the string into a list of substrings based on the separator.
  • string.strip(): Removes any leading or trailing whitespace characters from the string.

String Formatting

String formatting is the process of inserting values into a string. Python provides several ways to perform string formatting, including using the % operator, the format() method, and f-strings.

Using the % Operator

The % operator is a common way to format strings in Python. It allows you to specify placeholders in a string that will be replaced with values at runtime. The syntax for using the % operator is as follows:

string % values