Strings Copy

Strings in python are surrounded by either single quotation marks, or double quotation marks.

‘hello’ is the same as “hello”.

You can display a string literal with the print() function.

Example:

print(“Hello”)
print(‘Hello’)

Output:

Hello
Hello

Assign String to a Variable

Assigning a string to a variable is done with the variable name followed by an equal sign and the string.

Example:

a = “Hello”
print(a)

Output:

Hello

Multiline Strings

You can assign a multiline string to a variable by using three quotes.

Example:

a = “””String
can be
used as
multiline strings.”””
print(a)

Output:

String
can be
used as
multiline strings.

A sequence of characters terminated by a newline \n character or any other character. Individual characters in a string can be accessed using index positions.

Example:

a = “Hello, World!”
print(a[1])

Output:

e

Slicing

You can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string.

Example:

b = “Hello, World!”
print(b[2:5])

Output:

llo

String Concatenation

To concatenate, or combine, two strings you can use the + operator.

Example:

a = “Hello”
b = “World”
c = a + b
print(c)

Output:

HelloWorld