PYTHON TUTORIAL | PYTHON STRINGS



Modification of python string

How to change or delete a string?

Strings are immutable. This means that elements of a string cannot be changed once it has been assigned. We can simply reassign different strings to the same name.
n=”computer”
print(n)
computer
n[2]=’r’
print(n)

#error: TypeError: ‘str’ object does not support item assignment

Note: we can assign the string by another value but cannot change any character by using index.

n=”computer”
print(n)
n=’hello’
print(n)
computer
hello

n="computer"
print(n)
computer
n[2]='r'
print(n)

Output:

computer
Traceback (most recent call last):
  File "C:\Python37\tt1.py", line 3, in <module>
    computer
NameError: name 'computer' is not defined
>>> 
n="computer"
print(n)
n='hello'
print(n)

Output:

computer
hello
>>> 

Deletion of characters from a string

We cannot delete or remove characters from a string. But deleting the string entirely is possible using the keyword del.

n=”computer”
print(n)
computer
del n[1]
#Error :TypeError: ‘str’ object doesn’t support item deletion
print(n)

But we can delete the entire string

n=”computer”
print(n)
del n
print(n)
Note:
Error :NameError: name ‘n’ is not defined

n="computer"
print(n)
del n[1]
print(n)

Output:

computer
Traceback (most recent call last):
  File "C:\Python37\tt1.py", line 3, in <module>
    del n[1]
TypeError: 'str' object doesn't support item deletion
>>> 
n="computer"
print(n)
del n
print(n)

Output:

computer
Traceback (most recent call last):
  File "C:\Python37\tt1.py", line 4, in <module>
    print(n)
NameError: name 'n' is not defined
>>> 



Python String Operations

There are many operations that can be performed with string which makes it one of the most used datatypes in Python.

Concatenation of Two or More Strings

Joining two or more strings into a single one is called concatenation.
The + operator does this in Python. Simply writing two string literals together also concatenates them.
The * operator can be used to repeat the string for a given number of times.

str1 = ‘Hello’
str2 =’World!’

# using +

print(‘str1 + str2 = ‘, str1 + str2)
Output:
str1 + str2 = HelloWorld!

# using *

print(‘str1 * 3 =’, str1 * 3)
Output:
str1 * 3 = HelloHelloHello

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling

Tutorials

Technical Questions and Important programs

Interview Questions

C Programming
C++ Programming
Basic Python Tutorial
Advanced Python Tutorial
C Language
C++ Programming
Python programming
C Important Programs
C Interview Questions
C++ Interview Questions
Python Interview Questions
HTML Interview Questions