Python Strings

Python Strings

Anything between ‘ ‘ or “ “ in Python is String. Basically, Strings in Python are arrays of bytes representing Unicode characters. A character (‘a’) in Python is a String of length of 1. We do not have ‘char’ in Python. Everything from ‘a’ to ‘apple’ is a string.

As String is a type of Sequence in Python, every individual element of a String can be indexed depending on its position.

Forward & Reverse Indexing

Strings are immutable in Python, i.e. they can not be altered once created. Now’s let’s have a look at some basic operations which we can perform with Strings.

Basic Operations

  • Concatenation: Concatenation basically means adding two words or letters together to form a new single string. Whenever two strings are concatenated they are joined without a space between them.
  a = 'Pine'
  b = 'apple'
  c = a + b
  print(c)     #--> Pineapple
  • Repetition: Repetition simple means repeating a single string multiple times as defined by the user. But the repeated string is printed on the same line until defined by the user.
  a = 'Rama '
  print(a * 5)     #-->Rama Rama Rama Rama Rama
  • Slicing: As mentioned earlier, String is a part of Sequence in Python. So, Strings can be indexed orderly starting from index ‘0’. We can get access to individual string with the help of slicing.
  print(a[4])      #-->F
  • Slicing Ranging: Not only a single element but also a group of the element can be accessed with the help of the index.

  print(a[0:5])    #-->PLATF
  print(a[:5])     #-->PLATF

  print(a[5:9])    #-->ORUMA
  print(a[5:])     #-->ORUMA
  • Triple Quotes: In Python, apart from using Single Quote (‘ ‘) and Double Quote (” “), we can also use Triple Quote(”’ ”’ or “”” “””). They help strings to span multiple lines, including verbatim NEWLINEs, TABs, and any other special characters. They can be considered as ‘pre’ tags in HTML.
  #triple quotes
  a = 'apple'
  b = "apple"
  c = ''' apple a day
  keeps doctor away'''


String Methods

There are many methods available with the Strings Class. We will have look at some of the methods with examples in this tutorial, and rest other methods can be seen in the practice in Python Problem Blog Series, where we will solve many problem statements using multiple methods.

Let’s take these three variables into consideration for the rest of tutorial. We will be experimenting with these variables for all the String Method.

  a = 'Platforuma'
  b = 'saral'
  c = 'PYTHON'
  • len: len stands for Length. It returns integer values as an output.  It gives the total number of the element present in the String.

  print('Platforuma: ', len(a))
  #OUTPUT--> 10

  print('saral: ', len(b))
  #OUTPUT--> 5

  print('PYTHON: ', len(c))
  #OUTPUT--> 6
  • Count: Count returns integer values as an output.  It gives the number of count of the element.

  print('Count of "a" in Platforuma: ', a.count('a'))
  #OUTPUT--> 2

  print('Count of "a" in saral: ', b.count('a'))
  #OUTPUT--> 2

  print('Count of "S" in saral: ', b.count('S'))
  #OUTPUT--> 0

  print('Count of "s" in saral: ', b.count('s'))
  #OUTPUT--> 1

  print('Count of "a" in PYTHON: ', c.count('a'))
  #OUTPUT--> 0
  • islower: islower returns a boolean value of True/False if all the elements in the given String are lower-case.

  print('Platforuma: ', a.islower())
  #OUTPUT --> False

  print('saral: ', b.islower())
  #OUTPUT --> True

  print('PYTHON: ', c.islower())
  #OUTPUT --> False
  • isupper: isupper returns a boolean value of True/False if all the elements in the given String are upper-case.

  print('Platforuma: ', a.isupper())
  #OUTPUT --> False

  print('saral: ', b.isupper())
  #OUTPUT --> False

  print('PYTHON: ', c.isupper())
  #OUTPUT --> True
  • upper: upper returns a String after converting all the elements to UPPERCASE.

  print('Platforuma: ', a.upper())

  print('saral: ', b.upper())

  print('PYTHON: ', c.upper())
  • lower: lower returns a String after converting all the elements to lowercase.

  print('Platforuma: ', a.lower())
  #OUTPUT --> platforuma

  print('saral: ', b.lower())
  #OUTPUT --> saral

  print('PYTHON: ', c.lower())
  #OUTPUT --> python
  • replace: replace returns a String. It replaces an existing value with the given new value in the String.

  print('Platforuma: ', a.replace('a', 'XXX'), a)
  #OUTPUT --> PlXXXtforumXXX

  print('saral: ', b.replace('a', 'XXX'), b)

  print('PYTHON: ', c.upper(), c)
  • split: split can return either a String or a List depending upon how we use it.

  d = 'The quick bron fox jumps over the lazy dog'
  print(d.split(' '))
  #OUTPUT --> ['The', 'quick', 'bron', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

  e = 'Saral-Jain-Python-1996'
  f, g, h, i = e.split('-')
  print(f) --> Saral
  print(g) --> Jain
  print(h) --> Python
  print(i) --> 1996

  e = 'Saral-Jain-Python-1996'
  #OUTPUT --> ['Saral', 'Jain', 'Python', '1996']

We will be working with rest of the String Method in the Problem Solving Series.

Follow us on:

Source: Platforuma

Leave a Comment

Your email address will not be published. Required fields are marked *

EXTRA ₹999 OFF on Lock-Down Live Learning!

X myStickymenu