Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

01 Python Strings

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

01 Python Strings

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Strings

• Strings are contiguous set of characters.


• Python does not have a character data type; a single character is simply a string with a
length of 1.
• Square brackets can be used to access elements of the string.
• Strings are immutable. Updation or deletion of characters from a String is not allowed.

Creating a String

String1 = 'Welcome to the Python'


print("String with the use of Single Quotes: ")
print(String1)

# Creating a String with double Quotes


String1 = "Hello World"
print("\nString with the use of Double Quotes: ")
print(String1)

# Creating a String with triple Quotes


String1 = ‘’’It’s my life.’’’
print("\nString with the use of Triple Quotes: ")
print(String1)

# Creating String with triple Quotes allows multiple lines


String1 = ''' A for Apple
B for Ball
C for Cat'''
print("\nCreating a multiline String: ")
print(String1)

Accessing Characters

Negative index -5 -4 -3 -2 -1
String H E L L O
index 0 1 2 3 4

Individual characters of a String can be accessed by using Index. Index number starts at 0 for the 1st
character in the string.
Indexing also allows negative address references to access characters from the back of the String,
e.g. -1 refers to the last character, -2 refers to the second last character, and so on.

String Slicing

To access a range of characters in the String, the method of slicing is used. Slicing in a String is done
by using a Slicing operator (colon).

String1 = "Python Programming is fun"


print("Initial String: ")
print(String1)
# Printing 3rd to 12th character
print("\nSlicing characters from 3-12: ")
print(String1[3:12])

# Printing characters between 3rd and 2nd last character


print("\nSlicing characters between " + "3rd and 2nd last character: ")
print(String1[3:-2])

Deleting / Updating a String

In Python, Updation or deletion of characters from a String is not allowed. This will cause an error
because item assignment or item deletion from a String is not supported.

Although deletion of the entire String is possible with the use of a built-in del keyword.

This is because Strings are immutable, hence elements of a String cannot be changed once it has been
assigned. Only new strings can be reassigned to the same name.

You might also like