# Strings can be surrounded by either single or double quotation markssingle_quoted_string ='Hello, World!'double_quoted_string ="Hello, World!"# Strings using single or double quotes interchangeablyprint(single_quoted_string)print(double_quoted_string)# Multi-line strings using triple quotesmulti_line_string ="""This is a multi-line string.It spans multiple lines.This is often used for documentation."""print(multi_line_string)# Strings behave like lists: slicing, concatenation, and searching# Slicingsubstring = single_quoted_string[0:5]print("Sliced string:", substring)# Concatenationconcatenated_string = single_quoted_string +" How are you?"print("Concatenated string:", concatenated_string)# Searchingsearch_result ="World"in single_quoted_stringprint("Searching for 'World':", search_result)#Reassigning stringsingle_quoted_string ='Goodbye, World!'print("Reassigned string:", single_quoted_string)# Python treats single characters and strings of characters as the same typesingle_character ='A'print("Type of single_character:", type(single_character))print("Type of single_quoted_string:", type(single_quoted_string))
Hello, World!
Hello, World!
This is a multi-line string.
It spans multiple lines.
This is often used for documentation.
Sliced string: Hello
Concatenated string: Hello, World! How are you?
Searching for 'World': True
Reassigned string: Goodbye, World!
Type of single_character: <class 'str'>
Type of single_quoted_string: <class 'str'>
Strings are immutable, so we can’t change them after creation.
single_quoted_string[0] ='G'
---------------------------------------------------------------------------TypeError Traceback (most recent call last)
CellIn[4], line 1----> 1single_quoted_string[0] = 'G'TypeError: 'str' object does not support item assignment
We can concatenate strings using the + operator.
concatenated_string = single_quoted_string +" How are you?"print(concatenated_string)
Goodbye, World! How are you?
We can also use the * operator to repeat a string.