Python Strings
Python Basics
Python Introduction
Python Installation
Overview of Jupyter IDE
Identifiers & Reserved Keywords
Python Variables
Python Numbers
Python Operators
Python Operators and Arithmetic Operators
Comparison and Logical Operators
Assignment and Bitwise Operators
Identity and Membership Operators
Python Flow Control
if else if else statement
While Loop Statement
Python For Loop
Break and Continue Statement
Python Data Types
Python Strings
Python Strings Methods
Python Lists
Python Tuples
Python Dictionary
Python Functions
Introduction to Python Functions
Function Arguments
Recursion Function
Lambda/Anonymous Function
Python - Modules
Python Files
Python - Files I/O
Python - Exceptions Handling
Python - Debugging
What is Data Type?
It is an attribute that tells what kind of data that value or a variable can have it.
#Example: rollnumber = 12345 # rollnumber is integer name = "Anand" # name is string marks = 80.5 # marks is float
Standard Data Types
- Numbers
- Strings
- List
- Tuple
- Dictionary
Python Strings
Note: Python doesn’t have any separate data type for characters so they are represented as a single character string
How to create a python string?
#Sample Code singlequote = 'Python String' print(singlequote) doublequote = "Python Tutorials" print(doublequote) # triple quotes string can extend multiple lines triplequote = """Welcome to Vinoth Rathinam Tutorials""" print(triplequote) Output: Python String Python Tutorials Welcome to Vinoth Rathinam Tutorials
Accessing Values in Strings (Slicing)
#Sample Code myString = "Python for Machine Learning" #print first Character print(myString[0]) #slicing 11th to 18th characters print(myString[11:18]) # Using the defalut values - Use it as per the requirement print(myString[:]) print(myString[11:]) print(myString[:11]) Output: P Machine Python for Machine Learning Machine Learning Python for
Negative Indexing
# Sample code to slice the strings myString2 = "Python for Automation" #print last character using negative indexing print(myString2[-1]) print(myString2[-2]) print(myString2[-5]) #slicing 7th to 2nd last character print(myString2[7:-2]) Output: n o a for Automati
Index out of the range
# Sample code for IndexError myString3 = "Python" print(myString3[6]) Output: --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-25-4f411a63679a> in <module>() 1 # Sample code for IndexError 2 myString3 = "Python" ----> 3 print(myString3[6]) IndexError: string index out of range
# Sample code for TypeError print(myString2[1.5]) Output: --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-26-3f161d7da838> in <module>() 1 # Sample code for TypeError ----> 2 print(myString2[1.5]) TypeError: string indices must be integers
How to change or update a string ?
# Creating a string and checking the memory address str1 = "Python " print(str1) print("Memory location before update",id(str1)) # Trying to modify the string str1 += "Tutorials" # str1 = str1 + "Tutorials" print(str1) print("Memory location after update",id(str1)) Output: Python Memory location before update 1825268075800 Python Tutorials Memory location after update 1825268818328
# Updating the String Value myStr = "Happy Learning" print(myStr) myStr[4] = 's' Output: Happy Learning --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-29-85c0b5e71d2c> in <module>() 2 myStr = "Happy Learning" 3 print(myStr) ----> 4 myStr[4] = 's' TypeError: 'str' object does not support item assignment
How to Delete a String?
# Code to delete complete string del myStr print(myStr) Output: --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-31-ede3757523fc> in <module>() ----> 1 print(myStr) NameError: name 'myStr' is not defined