Creating a String in Python

Strings

 

 

Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable.

"Hello World"
"Creating a String in Python"

Creating a String in Python

# Defining a string in Python
# Creating a string in Python
# Using Single Quotes in string
String1='My name is Sam'
print(String1)

# Using Double Quotes in string
String2=("My Father's name is Mr.Ravi Sharma")
print(String2)

# Using Triple Quotes in String
String3=("""My Mother's name is Mrs.Reena""")
print(String3)

After running the program,the output would be :

My name is Sam
My Father's name is Mr.Ravi Sharma
My Mother's name is Mrs.Reena

Accesssing a Character in a String

n python String provides an [] operator to access any character in the string by index position. We need to pass the index position in the square brackets, and it will return the character at that index. This index position can be a positive or negative int value.

Indexing of String

Accessing string elements by positive index

In Python indexing of strings starts from 0 till n-1, where n is the size of string. So characters in string of size n, can be accessed from 0 to n-1.

Suppose we have a string i.e.

sampleStr="Hello"

Let’s access character at 4th index i.e.

sampleStr[4]

We can access the character by indexing.

# Access character at index 4
print( "Character at index 4 is : " , sampleStr[4] )

After running the program ,the output would be :

Character at index 4 is : o

This is because of the positive and negative indexing :

Accessing string elements by negative index

We can also access the character in string using negative indexing i.e.

  • string[-1] will return the last character of string
  • string[-2] returns the second last character of string
  • If string size is n then string[-n] will return the first character of string

For Example :

sampleStr = "Hello, this is a new era"
print( "Last character in string : " , sampleStr[-1] )
print( "Second Last character in string : " , sampleStr[-2] )
print( "First character in string : " , sampleStr[ -len(sampleStr) ] )

The output would be :

Output:
Last character in string :  a
Second Last character in string :  r
First character in string :  H

Modifying characters in string using []

Python strings are immutable, therefore we can not change content of string using [] operator. If we try to modify the string with [] then it will return error i.e.

sampleStr = "Hello, this is a new era"
'''
Modifying character in string by Index
As strings are immutable so we can modify the contents of string in python
'''
sampleStr[5] = 's'

It will show error like this :

TypeError: 'str' object does not support item assignment

We can access individual characters using indexing and a range of characters using slicing. Index starts from 0. Trying to access a character out of index range will raise an IndexError. The index must be an integer. We can’t use floats or other types, this will result into TypeError.If we try to access an index out of the range or use numbers other than an integer, we will get errors.

How to change  a string?

Strings are immutable. This means that elements of a string cannot be changed once they have been assigned. We can simply reassign different strings to the same name.

sampleStr = "Hello, this is a new era"
sampleStr[5] = 's'
TypeError: 'str' object does not support item assignment

But We can simply reassign different strings to the same name.

sampleStr = "Hello, this is a new World"
print(sampleStr)

The Output would be:

Hello, this is a new World
In python, the string data types are immutable which means a string value cannot be updated. We can verify this by trying to update a part of the string which will led us to an error.

 

%d bloggers like this: