Programming with Python

⌘K
  1. Home
  2. Docs
  3. Programming with Python
  4. Control Flow, Functions
  5. Strings

Strings

Strings

A string is a collection of one or more characters put in a single or double quotes or triple quotes

Example

‘python’
“python”
“”” python “””
  • In python, there is no character data type, a character is a string of length on
  • It is represented by str class
  • Individual character in a string is accessed using subscript (index). The index should always be an integer (positive or negative). Index starts from 0 and ends at n-1
  • Python will get the input at run time by default as a stringCharacters can be accessed using indexing and slicing operations
  • Strings are immutable i.e. the contents of the string cannot be changed after it is created

Example

Indexing

  • Square brackets can be used to access elements of the string
  • Positive indexing help in accessing the string from the beginning
  • Negative indexing helps in accessing the string from the end
  • Accessing the strings:
    • To display the first character either the string should be accessed using 0 or –n where n represents the length of the string
      • Str1[0] will display P
      • Str1[-6] will display P
    • To display second character
      • Str1[1] will display Y
      • Str1[-5] will display Y

Operations on strings

  • Indexing [ ]
  • Slicing [:]
  • Concatenation +
  • Repetition *
  • Membership(in, not in)

Creating a String

Creates a string variable

Example

str1 = "Learning Python"

Indexing

  • Accessing the item in position 0
  • Accessing the item in position 1
  • Accessing the last item

Example

print(str1[0])
L
 print(str1[1])
e
 print(str1[-1])
n

Slicing

  • Display items from position 1 to last
  • Display the items from position 2 to position 5-1
  • When given negative index, starts from -5 th position and printi the string till -1-1
  • Displays all the strings ranging from position 0
  • Displays all the strings ranging from position -6
  • Displays all the characters till position 4-1
  • [:] displays entire string
  • If the first index is greater than or equal to the second index, then empty string is returned

Example

print(str1[1:])
earning Python
print(str1[2:5])
arn
>>> str1 = "Learning Python"
>>> print(str1[-5:-1])
ytho
>>> print(str1[4:])
ning Python
>>> print(str1[-6:])
Python
>>> print(str1[1:9:2])
erig
>>> print(str1[1:9:3])
Eng
>>> str1[:4]
'Lear'
>>> str1[:]
'Learning Python'
>>> str1[3:3]
''

Concatenation (+)

Adding and printing the characters of two strings

Example

>>> print(str1+"is easy")
Learning Pythonis easy
>>> a = "SAVE"
>>> b = "TREES"
>>> print(a+b)
SAVETREES

Repetition (*)

Creates new strings by concatenating multiple copies of the same string

Example

>>> print(str1*2)
Learning PythonLearning Python

Membership operators

  • Using membership operators to check a particular character is in string or not.
  • in-Returns true if present else returns false
  • not in – Returns true if not present else returns false

Syntax

in, not in

Example

>>> "n" in str1
True
>>> "m" in str1
False
>>> "m" not in str1
True

String slices

  • A part of a string is called string slices
  • The process of extracting a sub string from a string is called slicing

Slicing

  • Display items from position 1 to last
  • Display the items from position 2 to position 5-1
  • When given negative index, starts from -5 th position and printi the string till -1-1
  • Displays all the strings ranging from position 0
  • Displays all the strings ranging from position -6
  • Displays all the characters till position 4-1
  • [:] displays entire string
  • If the first index is greater than or equal to the second index, then empty string is returned

Example

print(str1[1:])
earning Python
print(str1[2:5])
arn
>>> str1 = “Learning Python”
>>> print(str1[-5:-1])
ytho
>>> print(str1[4:])
ning Python
>>> print(str1[-6:])
Python
>>> print(str1[1:9:2])
erig
>>> print(str1[1:9:3])
Eng
>>> str1[:4]
‘Lear’
>>> str1[:]
'Learning Python'
>>> str1[3:3]
''

Immutability

  • Python strings are immutable as they cannot be changed after they are created
  • Thus, subscript operator [ ], cannot be used on the left side of an assignment

Element Assignment

TypeError: ‘str’ object does not support item assignment

Example

>>> str1="python"
>>>  str1[0]="z"

Element Deletion

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

Example

>>> str1="python"
>>> del str1[0]

Loading

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments