Strings in Python 3

Python 3 – Strings



Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python allows either pair of single or double quotes.


Creating strings is as simple as assigning a value to a variable. For example

var1 = 'Hello World!'
var2 = "Python Programming"


Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 to the end.

The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator. 


Accessing Values in Strings
Python does not support a character type; these are treated as strings of length one, thus also considered a substring.



To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring. For example-


  • Slice operators : [ ] and [:]
  • Indexes : starts from 0 in forward direction, -1 in reverse direction.
  • concatenation operator : +
  • repetition operator : *

Example : let's take -  Hello, World!






and one more example here -       

                               
 and the output is :



Updating Strings

You can "update" an existing string by (re)assigning a variable to another string. The new value can be related to its previous value or to a completely different string altogether.

For example-


#!/usr/bin/python3

var1 = 'Hello World!'
print ("Updated String :- ", var1[:6] + 'Python')

it's Out put :

Updated String :- Hello Python