google.com, pub-8786015629279405, DIRECT, f08c47fec0942fa0 String in Python Programming

String in Python Programming

0

 

Strings are arrays of bytes in Python that represent Unicode (Letter, Character & Special character) characters. However, because Python don't have any character data type, a single character is simply a one-length string. Square brackets can be used to access the string's components.

String have some 

Create a String or Declare a String

In Python, single quotes, double quotes, and even triple quotes can be used to construct strings.

For Example

 

# Write a program to Declare String in Python

 

# Declare a String with Single Quotes

Str1=’Welcome to the a2pstudy’

print(“This is the Example of Single Quotes String:”, Str1)

 

# Declare a String with Double Quotes

Str2=” I’m a2pstudy”

print(“This is the Example of Double Quotes String:”, Str2)

 

# Declare a String with Triple Quotes

Str3=’’’ I’m “a2pstudy” ’’’

print(“This is the Example of Triple Quotes String:”, Str3)

 

# Declare a String with Triple Quotes, Allow Multiple Line

Str4=’’’ Hi

              Welcome

              To

              The a2pstudy ’’’

print(“This is the Example of Triple Quotes with Multi Line String:”, Str4)

 

 

Output :

 

This is the Example of Single Quotes String:

Welcome to the a2pstudy

 

This is the Example of Double Quotes String:

I’m a2pstudy

 

This is the Example of Triple Quotes String:

I’m “a2pstudy”

 

This is the Example of Triple Quotes with Multi Line String:

Hi

              Welcome

              To

              The a2pstudy

 

 

 

Accessing values in Python

As we know that Python don’t work on character data type. Its works only String but we can access a character using index of the String.

Individual characters in a String can be retrieved using the Indexing function in Python. Negative address references, such as -1 for the final character, -2 for the second last character, and so on, allow negative address references to access characters from the back of the String.

 

An IndexError will occur if an index is accessed outside of the range. Only integers, floats, and other kinds that will trigger a TypeError are allowed as indexes.

Here we can see the Example of the Negative index for better understanding.

Reverse Indexing

 

# Write a program to Access a character/String in Python

 

# Access the Sting  

Str1=” a2pstudy”

print(“Access Complete String:”, Str1)

 

# Access a Character Using Index Method in Python.

Str2=’a2pstudy’

print(“Access First Character:”, Str2[0])

 

 

Str3=’’’a2pstudy’’’

print(“Access Last Character:”, Str3[7])



Output :

 

Access Complete String:

a2pstudy

 

Access First Character:

a

 

Access Last Character:

y

 

Accessing values with String Slice in Python

The slicing technique is used to access a range of characters in the String. The Slicing operator is used to slice a String (colon).

For Example:

 

# Write a program to Access character/String using String Slice in Python

 

# Access the Sting  

Str1=” a2pstudy”

print(“Access Complete String:”, Str1)

 

# Access a Character from 2 to 5 positions.

print(“Access a Character from 2 to 5 positions:”, Str1[2:6])

 

# Access a reverse string.

print(“Access a reverse string:”, Str1[0:-1])



Output :

 

Access Complete String:

a2pstudy

 

Access a Character from 2 to 5 positions:

pstu

 

Access a reverse string:

ydutsp2a

 


Updating/Deletion in a String in Python

 Strings are immutable, which means that once they've been allocated, they can't be modified. The same name can only be reassigned to new strings.

 

So,Updating or deleting characters from a String is therefore forbidden. Because item assignment or deletion from a String is not supported, this will result in an error. Although there is a built-in del keyword that may be used to delete the entire String.

 

1. Updating :


1.   1. Updating a Character from a given String.

 

 

# Write a Program to Update character from a given String in Python

 String1 = "www.a2pstudy.blogspot.com"

print("Initial String is : ")

print(String1)

 # Updating a character from the String

 String1[4] = 'p'

print("\n Updating a  character from the given String at 4th Index Position is : ")

print(String1)

 Output:

 Error: (Due to the String immutable property, we can’t update a character.)

 Traceback (most recent call last):

File “/home/360bb1830c83a918fc78aa8979195653.py”, line 11, in

String1[4] = ‘p’

TypeError: ‘str’ object does not support item assignment

 

 

2.   2. Updating Complete String from a given String.



# Write a Program to Update Entire/Complete String from

# a given String in Python

 String1 = "www.a2pstudy.blogspot.com"

print("Initial String is : ")

print(String1)

 # Updating Complete String  from the String

 String1="www.a2pstudy.com"

print("\n Updating Complete String from the given String : ")

print(String1)

 Output

 Initial String is :

www.a2pstudy.blogspot.com

 Updating Complete String from the given String :

www.a2pstudy.com

 

 

2. Deletion:

 

1.   1. Delete a character form the given String

 

 

# Write a Program to Delete a character from a given String in Python

 String1 = "www.a2pstudy.blogspot.com"

print("Initial String is : ")

print(String1)

 # Updating a character from the String

 del String1[4]

print("\n Deleting a  character from the given String at 4th Index Position is : ")

print(String1)

 Output:

 Error: (Due to the String immutable property, we can’t Delete a character.)

 Traceback (most recent call last):

File “/home/499e96a61e19944e7e45b7a6e1276742.py”, line 11, in

del String1[2]

TypeError: ‘str’ object doesn’t support item deletion

 

 

2. Deleting Complete String from a given String.

 

Using the del keyword to delete the entire string is feasible. Furthermore, attempting to print the string will result in an error since String has been erased and is not available for printing.

 

 

# Write a Program to Delete Entire/Complete String from

# a given String in Python

 String1 = "www.a2pstudy.blogspot.com"

print("Initial String is : ")

print(String1)

 # Delete Complete String  from the String

 String1="www.a2pstudy.com"

print("\n Deleting Complete String from the given String : ")

print(String1)

 Output: 

 Error:

 Traceback (most recent call last):

File “/home/e4b8f2170f140da99d2fe57d9d8c6a94.py”, line 12, in

print(String1)

NameError: name ‘String1’ is not defined

  

 

Escape Sequencing in Python

 

Some time, strings have single or double quotas. So there is an error like SyntaxError.

There are two Methods to remove that error.

1.Use of Triple Quotas

In this Methord, we just put all the string between the triple inverted comma(‘’’ ‘’’). Like ‘’’ it’s a nice day ’’’.

 

For example:



# Write a Program for Escape Sequencing

# of String in Python Using Triple Quotas.

 

# String with Triple Quotas

 

Str1 = '''it's a "rainy" day'''

print("Initial String with use of Triple Quotes: ")

print(Str1)

 

 

Output:

String with use of Triple Quotes:

it's a "rainy" day

 

 

2.Use of Escape Sequencing

 

Escape sequences begin with a backslash and can be read in a variety of ways. If a string is represented by single quotes, all single quotes in the string must be escaped, and the same is true for double quotes.

 

For example:

 

# Write a Program for Escape Sequencing

# of String in Python.

 

# String with Triple Quotas

 

Str1 = '''it's a "rainy" day'''

print("Initial String with use of Triple Quotes: ")

print(Str1)

 

# Escaping Single Quote using backslash

Str1 = 'it\'s a "rainy" day'

print("\nEscaping Single Quote: ")

print(Str1)

 

# Escaping Double Quotes using backslash

Str1 = "it's a \"rainy\" day"

print("\nEscaping Double Quotes: ")

print(Str1)

 

# Printing Paths with the

# use of Escape Sequences or using backslash

Str1 = "C:\\Python\\a2pstudy\\"

print("\nEscaping Backslashes: ")

print(Str1)

 

Output:

Initial String with use of Triple Quotes:

it's a "rainy" day

Escaping Single Quote:

it's a "rainy" day

 

Escaping Double Quotes:

it's a "rainy" day

 

Escaping Backslashes:

C:\Python\a2pstudy\


Formatting of Strings

The format() method, which is a very versatile and powerful tool for formatting Strings, can be used to format strings in Python. Curly braces are placeholders in the String Format function that can hold arguments according to position or keyword to set the order.

 

# Python Program for Formatting Strings

 

# Formatting with Default order

Str1= "{} {} {}".format('www', '.', 'a2pstudy',’.’,’com’)

print("Print String in default order: ")

print(Str1)

 

# Formatting with Positional order

Str1 = "{4} {2} {1} {0} {3}".format('.', 'a2pstudy',’.’,’com’ ,'www')

print("\nPrint String in Positional order: ")

print(Str1)

 

# Formatting with Keyword order

Str1 = "{w} {a} {c}".format(w='www', a='.a2pstudy', c='.com')

print("\nPrint String in order of Keywords: ")

print(Str1)

 

 

Output:

Print String in default order:

www.a2pstudy.com

 

Print String in Positional order:

www.a2pstudy.com

 

Print String in order of Keywords:

www.a2pstudy.com

 

 


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Thank you for your interest 😊

We will back shortly after reviewing...

Thank you for your interest 😊

We will back shortly after reviewing...

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top