Definition

  • In Python, a string is a sequence of characters enclosed within single (‘ ‘), double (” “), or triple quotes (”’ ”’ or “”” “””).
  • Strings are immutable, i.e. they cannot be changed after creation.
  • Strings in Python are powerful and come with many built-in methods for manipulation. 

String Creation/Declaration

  • We can define strings using:-
str1 = ‘Hello India’ # Using Single quotes
str2 = “Hello India” # Using Double quotes
str3 = “1234”
str4 = “Hello 242”
str5 = ”’For Multiline String”’ # Triple quotes (Applicable for multi-line strings)

print(str1, str2, str3,str4,str5)
print()
print(str1)
print(str2)
print(str3)
print(str4)
print(str5)
Output:
Hello India Hello India 1234 Hello 242 For Multiline String

Hello India
Hello India
1234
Hello 242
For Multiline String

Accessing Characters in a String

  • Strings in Python are indexed and support slicing features.
  • For example – 
s = “Python”
print(s[0])
print(s[-1])
print(s[1:4])
print(s)
print(s[0:-1])
print(s[0:5])
print(s[0:6])
Output:
P
n
yth
Python
Pytho
Pytho
Python

String Operations

  • Python provides several operations on strings:-

a) Concatenation (+)

s1 = “Hello”
s2 = “India”
print(s1 + ” “ + s2) # Output: Hello India

b) Repetition (*)

print(“Hello “ * 3) # Output: Hello Hello Hello

c) Membership (in, not in)

print(“Py” in “Python”) # Output: True
print(“Java” not in “Python”) # Output: True

Built-in String Methods

  • Python provides many methods to manipulate strings, but some common are:-
Methods Descriptions Examples
len() Find the length of the String s = "Hello" print(len(s)) # Output: 5
lower() Converts to lowercase "Hello".lower()"hello"
upper() Converts to uppercase "hello".upper()"HELLO"
strip() Removes whitespace " Hello ".strip()"Hello"
replace(old, new) Replaces substring "Hello".replace("H", "J")"Jello"
split(delim) Splits string into list "Hello World".split()["Hello", "World"]
join(iterable) Joins elements of iterable "-".join(["Hello", "World"])"Hello-World"
find(sub) Finds index of substring "Python".find("th")2

String Formatting

Python provides multiple ways to format strings:

a) Using format()

name = “Soha” age = 18
print(“My name is {} and I am {} years old.”.format(name, age))

b) Using f-strings (In Python 3.6+)

name = “Soha” age = 18
print(f“My name is {name} and I am {age} years old.”)

c) Using % formatting (Old method)

print(“My name is %s and I am %d years old.” % (name, age))

Escape Sequences

  • Escape sequences are special characters used in strings to format the strings in the desired way:
Escape Sequence Description Example
\n Newline "Hello\nWorld"
\t Tab space "Hello\tWorld"
\' Single quote 'It\'s Python'
\" Double quote "She said \"Hello\""
\\ Backslash "C:\\Users\\Name"

String Immutability

  • Since strings are immutable in Python, i.e. they cannot be changed after creation directly.
s = “Python”
s[0] = “J” # Error! Strings are immutable hence not added

But, to modify a string indirectly, we must do it in a new way: –

s = “Python”
s = “J” + s[1:] # Output: “Jython”

Looping in a String

  • We can iterate through a string using a loop as below:-
s = “Python”
for x in s:
print(x, end=” “) # Output: P y t h o n

Loading

Categories: Python Theory

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.