String in Python

String in Python

Python String

string in python,python split,python split string,python string to int,python int to string,python list to string,int to string python,python string methods,python uppercase

In Python, Strings are arrays of bytes representing Unicode characters. However, 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. And they are immutable.

Creating a String

Strings in Python can be created using single quotes or double quotes or even triple quotes. Lets see by an example how we can do this.

code :

# Python Program for
# Creation of String
  
# Creating a String 
# with single Quotes
String1 = 'Welcome to the Code World'
print("String with the use of Single Quotes: ")
print(String1)
  
# Creating a String
# with double Quotes
String1 = "I'm a Coder"
print("\nString with the use of Double Quotes: ")
print(String1)
  
# Creating a String
# with triple Quotes
String1 = '''I'm a Coder and i will live i this world'''
print("\nString with the use of Triple Quotes: ")
print(String1)
  
# Creating String with triple
# Quotes allows multiple lines
String1 = '''Code
            For
            Jb'''
print("\nCreating a multiline String: ")
print(String1)


output :

String with the use of Single Quotes: Welcome to the Code World String with the use of Double Quotes: I'm a Coder String with the use of Triple Quotes: I'm a Coder and I will live in this world Creating a multiline String: Code For Job



Accessing characters in Python


In Python, individual characters of a String can be accessed by using the method of Indexing. Indexing 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.
While accessing an index out of the range will cause an IndexError. Only Integers are allowed to be passed as an index, float or other types will cause a TypeError.

string in python,python split,python split string,python string to int,python int to string,python list to string,int to string python,python string methods,python uppercase


code :

# Python Program to Access
# characters of String
  
String1 = "codedudle"
print("Initial String: ")
print(String1)
  
# Printing First character
print("\nFirst character of String is: ")
print(String1[0])
  
# Printing Last character
print("\nLast character of String is: ")
print(String1[-1])


output :

Initial String: codedudle First character of String is: c Last character of String is: e



String Slicing


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

code :

 # Python Program to

# demonstrate String slicing
  
# Creating a String
String1 = "codedudle"
print("Initial String: "
print(String1)
  
# Printing 3rd to 12th character
print("\nSlicing characters from 3-8: ")
print(String1[3:8])
  
# Printing characters between 
# 3rd and 2nd last character
print("\nSlicing characters between " +
    "3rd and 2nd last character: ")
print(String1[3:-2])


output :

Initial String:
codedudle Slicing characters from 3-12: edudle Slicing characters between 3rd and 2nd last character: edud


Deleting/Updating from 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 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.

Updation of a character:

code :

# Python Program to Update
# character of a String
  
String1 = "Hello, I'm a coder"
print("Initial String: ")
print(String1)
  
# Updating a character 
# of the String
String1[2] = 'p'
print("\nUpdating character at 2nd Index: ")
print(String1)


It will give an error as we have already seen that strings are immutable and updating a character is not available.

but the deletion of entire string is possible. But we will update the entire String.

Lets see how we can do this.

code :

# Python Program to Update
# entire String
  
String1 = "Hello, I'm a coder"
print("Initial String: ")
print(String1)
  
# Updating a String
String1 = "Welcome to the hello World"
print("\nUpdated String: ")
print(String1)


output :

Initial String: Hello, I'm a Geek Updated String: Welcome to the hello World


As we mentioned above that a String is immutable, so we cant delete the character from the string.

Lets see a example related to it.

# Python Program to Delete
# characters from a String
  
String1 = "Hello, I'm a coder"
print("Initial String: ")
print(String1)
  
# Deleting a character 
# of the String
del String1[2
print("\nDeleting character at 2nd Index: ")
print(String1)

But it will give a error

Deleting Entire String:

Deletion of entire string is possible with the use of del keyword. Further, if we try to print the string, this will produce an error because String is deleted and is unavailable to be printed.

We will see a code related to it . It will again give an error.

# Python Program to Delete
# entire String
  
String1 = "Hello, I'm a coder"
print("Initial String: ")
print(String1)
  
# Deleting a String
# with the use of del
del String1 

print("\nDeleting entire String: ")
print(String1)


Escape Sequencing in Python

While printing Strings with single and double quotes in it causes SyntaxError because String already contains Single and Double Quotes and hence cannot be printed with the use of either of these. Hence, to print such a String either Triple Quotes are used or Escape sequences are used to print such Strings.
Escape sequences start with a backslash and can be interpreted differently. If single quotes are used to represent a string, then all the single quotes present in the string must be escaped and same is done for Double Quotes.



# Python Program for
# Escape Sequencing 
# of String
  
# Initial String
String1 = '''I'm a "coder"'''
print("Initial String with use of Triple Quotes: ")
print(String1)
  
# Escaping Single Quote 
String1 = 'I\'m a "coder"'
print("\nEscaping Single Quote: ")
print(String1)
  
# Escaping Doule Quotes
String1 = "I'm a \"coder\""
print("\nEscaping Double Quotes: ")
print(String1)
  
# Printing Paths with the 
# use of Escape Sequences
String1 = "C:\\Python\\coder\\"
print("\nEscaping Backslashes: ")
print(String1)

output :

Initial String with use of Triple Quotes: I'm a "coder" Escaping Single Quote: I'm a "coder" Escaping Double Quotes: I'm a "coder" Escaping Backslashes: C:\Python\coder\


Formatting of Strings

Strings in Python can be formatted with the use of format() method which is very versatile and powerful tool for formatting of Strings. Format method in String contains curly braces {} as placeholders which can hold arguments according to position or keyword to specify the order.

Lets see a example

# Python Program for
# Formatting of Strings
  
# Default order
String1 = "{} {} {}".format('Code', 'For', 'Job')
print("Print String in default order: ")
print(String1)
  
# Positional Formatting
String1 = "{1} {0} {2}".format('Code', 'For', 'Job')
print("\nPrint String in Positional order: ")
print(String1)
  
# Keyword Formatting
String1 = "{j} {f} {c}".format(c = 'Code', f = 'For', j = 'Job')
print("\nPrint String in order of Keywords: ")
print(String1)

output :

Print String in default order: Code For Job Print String in Positional order: For Code Job Print String in order of Keywords: Code For Job

In this article i have given brief summary of String data type in Python

Next Article : Tuple in Python



Recommended Post : Introduction to Data types








#python online
#python programming
#python list
#learn python
#python requests
#introduction to data science in python
#free python tutorial
#learn python basics
#python study
#python and ai
#python computer science
#learn python step by step
#best course to learn python































































Post a Comment

2 Comments

If you have any doubt, Please let me know.