String | Long String | Concatenation Multiplication | String Indexing | Sting’s Immutability
Hello, my name is Kishan Tongrao. We are going to discuss the String data types in Python programming language.
What is String data type in Python?
- In Python, the string data type is used to represent a sequence of characters. Strings are enclosed within either single quotes (‘…’) or double quotes (“…”).
- Strings are immutable, which means that once a string is created, it cannot be modified. However, you can create a new string by combining two or more strings using the concatenation operator (+) or by repeating a string using the multiplication operator (*).
# Code — Use the below code to create a string variable and check its data type.
# string variable
str_var = "Name"
str_var1 = 'Name'
print(str_var)
print(type(str_var))
print(str_var1)
print(type(str_var1))
# Name
# <class 'str'>
# Name
# <class 'str'>
What is a Long String in Python?
- In Python, a long string is a string that spans multiple lines. You can create a long string by enclosing it within triple quotes (either three single quotes or three double quotes)
# Code — Use the below code to create a long string
long_string = '''This is a long string with escape characters
that spans multiple lines.\n
You can include multiple sentences\t
and paragraphs in a long string.'''
long_string = """This is a long string with escape characters
that spans multiple lines.\n
You can include multiple sentences\t
and paragraphs in a long string."""
long_string = 'This is the first line of a long string.' + \
'This is the second line of a long string.' + \
'This is the third line of a long string.'
String Concatenation and Multiplication
Creating a new string by combining two or more strings is called String Concatenation. Along with Concatenation, we can use Multiplication to generate new string characters.
# Code — Use the below code for String Concatenation and Multiplication
# concatenation
greeting = 'Hello'
name = 'John'
message = greeting + ', ' + name + '!'
print(message) # output: Hello, John!
# multiplication
symbol = '-'
line = symbol * 10
print(line) # output: ----------
String Indexing | Positive and Negative Indexing
We can use positive integers and negative integers as brackets for the string to access its characters and we are calling it String Indexing. If we are using Positive indexes then it’s Positive String Indexing and if we are using Negative indexes then it’s Negative String Indexing.
# Code — Use the below code to see Positive and Negative String Indexing.
# positive indexing
var_str = "STRING"
print(var_str)
# +ve index
print("Positive index results")
print(var_str[2])
print(var_str[:])
print(var_str[2:])
print(var_str[:2])
print(var_str[::2]
# STRING
# Positive index results
# R
# STRING
# RING
# ST
# SRN
# negative indexing
var_str = "STRING"
print(var_str)
# -ve index
print("Negative index results")
print(var_str[::-1])
print(var_str[-1])
print(var_str[:-1])
print(var_str[-2:])
# STRING
# Negative index results
# GNIRTS
# G
# STRIN
# NG
Strings Immutable Nature
Now let’s see using the below code if String is immutable in nature or not and its workaround.
# Code — Use the below code to demonstrate the String’s Immutable nature.
# string immutable
var_str = "STRING"
# try to change
var_str[1] = "P"
print(var_str)
# TypeError: 'str' object does not support item assignment
# workaround
print(var_str[0] + "P" + var_str[2:])
# SPRING
So that’s it. Thank You