Skip to main content

Strings




Strings are used in Python to record text information, such as names. 

Strings in Python are actually a sequence, which basically means Python keeps track of every element in the string as a sequence. 

For example, Python understands the string "hello' to be a sequence of letters in a specific order. This means we will be able to use indexing to grab particular letters (like the first letter, or the last letter).

This idea of a sequence is an important one in Python and we will touch upon it later on in the future.

In this blog we'll learn about the following:
  • Creating Strings
  • Printing Strings
  • String Methods
  • String Indexing
  • String Slicing
  • String Properties

Creating String:

1] Strings can be created as below using single quotes or double quotes
strVar = 'Python'
strVar1 = "python"

2] A multi-line string can declared with triple quotes as below:
strVar2 = """python language
is easy to learn"""

3] A string declaration with quotes should be handled properly. For example, if we created any quote[either single or double quotes] line, we have to use combinations of double and single quotes to get the complete statement like below
print("hello Jane's ")
print('Hello Mr" hw are u')
o/p - 
hello Jane's 
Hello Mr" hw are u

Printing String: 

1] For above declared strings, output will be printed like below: 

print("Output of String Var:", strVar)
print("Output of String Var1:", strVar1)
print("Output of String Var2:", strVar2)
o/p -
Output of String Var: Python 
Output of String Var1: python 
Output of String Var2: python language 
is easy to learn
2] We can use the .format() method to add formatted objects to printed string statements

print("Curly bracket is to insert another string: {}".format('New string'))
o/p - 
Curly bracket is to insert another string: New string

Basic Built-in String methods

1] replace() is to replace the strings from string variable
Syntax will be, String_name.replace("text what to replace", "New text")

strVarNew = strVar2.replace("language", "programming language")
print("\nString Var2 is:", strVar2)
print("Output of String Var New:", strVarNew)
o/p -
String Var2 is: python language
is easy to learn 
Output of String Var New: python programming language 
is easy to learn


2] len() to find length the string
print("\nString Var1 is :", strVar1)
print("Length of string Var 1:", len(strVar1))
o/p -
String Var1 is : python 
Length of string Var 1: 6

String indexing

Since strings are a sequence, which means Python can use indexes to call parts of the sequence. Let's learn how this works.

print("\nString var1 is", strVar1)
print("First index in str var1 is", strVar1[0])
print("Second index in str var1 is", strVar1[1])
o/p -
String var1 is python 
First index in str var1 is p 
Second index in str var1 is y

String Slicing

This will be defined in format as [start index: stop index: step size]
  • Here start index, the index from where slice need to start
  • Stop index is upto where slicing need to done. So this index value wont include in slicing output
  • step size, by default it will be 1, so if we want to change we can include the step size
  • Slicing can be used to reverse the string also. 
print("\nString Var is :", strVar)
print("Start index slicing - string var", strVar[1:])
print("Stop index slicing - string var:", strVar[:3])
print("Using step size - string Var:", strVar[::3])
print("Using slicing to reverse a string:", strVar[::-1])
o/p -
String Var is : Python 
Start index slicing - string var ython 
Stop index slicing - string var: Pyt 
Using step size - string Var: Ph 
Using slicing to reverse a string: nohtyP

String Properties

1] Strings have an important property known as immutability. This means that once a string is created, the elements within it can not be changed or replaced.

Trying to assigned string index of strVar, got "typeError"

strVar[0] = 'H'
o/p -
Traceback (most recent call last): 
 File "string.py", line 41, in <module> 
 strVar[0] = 'H' 
TypeError: 'str' object does not support item assignment


2] Since we can't replace existing string, we can add string to existing string. This can be done through concatenation

strVar = strVar + "maestro"
print("\nConcanate string output:", strVar)
o/p -
Concanate string output: Pythonmaestro

Comments