Strings in Python

Strings in Python

Strings in Python are collection of words enclosed in single quotation marks or double quotation marks. Python does not have a character type to store single characters. A character in Python is a string of length 1.

 

 

Creating Strings in Python

Strings in Python are created by enclosing them within single quotation marks, double quotation makrs and even triple quotation marks.

 

# Python program to exemplify string creation

# Creating a string with single quotation marks
string1='Welcome to Studyexperts!'
print(string1)

# Creating a string with double quotation marks
string2="Welcome to Studyexperts!"
print(string2)

# Creating a string with triple quotation marks
string3='''Welcome to Studyexperts!'''
print(string3)

# Creating a string with triple quotation marks
# triple quotation marks allow creation of multi-line string
string4='''Welcome to 
Studyexperts!'''
print(string4)

Output:

Welcome to Studyexperts!
Welcome to Studyexperts!
Welcome to Studyexperts!
Welcome to 
Studyexperts!

 

 

Accessing Characters of Strings in Python

In Python, individual characters of a string are accessed or acquired by the technique of indexing which makes use of both the positive and negative address references. Negative indexing starts from -1 and returns the first character of the string from the end. Positive indexing starts from 0 and returns the first character of the string from the beginning. Indexing a string may also lead to some errors like IndexError or TypeError which occurs when accessing an index out of range and passing anything but integers as an index respectively.

 

# Python program to exemplify character access of a string

var_string="Welcome to StudyExperts!"
print("The String is ",var_string)

# Displaying first character
print("First character of the string is ",var_string[0])

# Displaying last character
print("Last character of the string is ",var_string[-1])

Output:

The String is  Welcome to StudyExperts!
First character of the string is  W
Last character of the string is  !

 

 

String Slicing

String slicing is done when a group of characters of s string is need to be obtained and is done using the slicing operator colon (:).

 

# Python program to exemplify string slicing

var_string="Welcome to StudyExperts!"
print("The String is ",var_string)

# Displaying characters between indexes 3 and 15
print("Characters of the string between indexes 3 and 15 is ",var_string[3:15])

# Displaying characters between indexes 3 and -5
print("Characters of the string between indexes 3 and -5 is ",var_string[3:-5])

Output:

The String is  Welcome to StudyExperts!
Characters of the string between indexes 3 and 15 is  come to Stud
Characters of the string between indexes 3 and -5 is  come to StudyExp

 

 

Deleting/Updating a String

In Python, string data type are immutable which means characters of a string cannot be updated or deleted. Though entire string can be deleted using a built-in keyword del and an already created string variable can be assigned a new string object.

Updating a character:

# Python program to exemplify updating a single character of a string 

var_string="Welcome to StudyExperts!"
print("The String is ",var_string)

# Updating a character at index 2
var_string[2]="s"
print("The String after updating a single character is ",var_string)

Output:

The String is  Welcome to StudyExperts!
Traceback (most recent call last):
  File "main.py", line 7, in <module>
    var_string[2]="s"
TypeError: 'str' object does not support item assignment

 

Updating entire string:

# Python program to exemplify updating string 

var_string="Welcome to StudyExperts!"
print("The String is ",var_string)

# Updating entire string
var_string="s"
print("Updated string is ",var_string)

Output:

The String is  Welcome to StudyExperts!
Updated string is  s

 

Deleting a character:

# Python program to exemplify deleting a character from a string 

var_string="Welcome to StudyExperts!"
print("The String is ",var_string)

# deleting a character at index 4
del var_string[4]
print("Updated string is ",var_string)

Output:

The String is  Welcome to StudyExperts!
Traceback (most recent call last):
  File "main.py", line 7, in <module>
    del var_string[4]
TypeError: 'str' object doesn't support item deletion

 

Deleting entire string:

Trying to display the string after deleting it using the del keyword will result in an error because the string no longer exists.

 

# Python program to exemplify deleting entire string 

var_string="Welcome to StudyExperts!"
print("The String is ",var_string)

# deleting entire string
del var_string


Output:

The String is  Welcome to StudyExperts!

 

 

Escape Sequencing in Python

Escape sequences are special characters used inside a string so that they can be executed and not ignored as a string. They start with a backslash. If a string is made using single quotation marks, then any single quotation mark of a string to be interpreted as a string should be escaped with a backslash character. To ignore escape sequences in a string, it is preceded by a r or R and is made a raw string.

 

# Python program to exemplify escape sequencing in Python 

var_string="I\"m at StudyExperts!"
print("The String is ",var_string)

var_string="I'm at StudyExperts!"
print("The String is ",var_string)

Output:

The String is  I"m at StudyExperts!
The String is  I'm at StudyExperts!

 

 

Formatting of Strings in Python

In Python, strings are formatted using a method called format() which works with sets of curly braces {} that act as placeholders to hold arguments according to position or keyword that specify the order.

 

# Python program to exemplify string formatting in Python 

# Default order of string formatting
var_string="{} {} {}".format("You're","at","StudyExperts!")
print(var_string)

# Positional formatting of string
var_string="{1} {0} {2}".format("at","You're","StudyExperts!")
print(var_string)

# Keyword formatting of string
var_string="{a} {b} {c}".format(b="at",a="You're",c="StudyExperts!")
print(var_string)

Output:

You're at StudyExperts!
You're at StudyExperts!
You're at StudyExperts!

 

Integers such as binary, octal, etc., and floats can be approximated or displayed in their exponential form using this method.

 

var_string="{}".format(56)
print("Binary representation of 56 is: ",end="")
print(var_string)

var_string="{0:e}".format(34.567)
print("Exponent representation of 34.567 is: ",end="")
print(var_string)

var_string="{0:.2f}".format(1/7)
print("1/7 with 2 precision is: ",end="")
print(var_string)

Output:

Binary representation of 56 is: 56
Exponent representation of 34.567 is: 3.456700e+01
1/7 with 2 precision is: 0.14

 

 

len() Method

This method is used to find the number of characters a string is made up of.

 

# Python program to exemplify len() method

var_string="StudyExperts"
length=len(var_string)
print(var_string," is of length ",length)

Output:

StudyExperts  is of length  12

 

 

String Concatenation

Two or more strings can be concatenated using the concatenation operator “+”. Concatenation of strings do not result in modification of any string but a new string since strings in Python are immutable.

 

# Python program to exemplify string concatenation

var1="Study"
var2="Experts"
var_string=var1+var2
print(var1)
print(var2)
print(var_string)

Output:

Study
Experts
StudyExperts

 

Share this post

Leave a Reply

Your email address will not be published.