How to use strings in Python

Strings are a type of data used in Python to represent text-based data. Strings can be manipulated and used in a variety of ways in Python.

To create a string in Python, enclose the text in quotation marks. For example:

my_string = “Hello World!”

To access individual characters in a string, use the index operator []. For example:

my_string[0] # returns ‘H’

To concatenate strings together, use the + operator. For example:

my_string = “Hello” + ” ” + “World!” # returns “Hello World!”

To repeat a string multiple times, use the * operator. For example:

my_string = “Hello” * 3 # returns “HelloHelloHello”

To find the length of a string, use the len() function. For example:

len(my_string) # returns 11

To search for a substring within a string, use the in operator. For example:

“World” in my_string # returns True