A string is a sequence of characters. A character is simply a symbol. For example, the English language has 26 characters.
Computers do not deal with characters, they deal with numbers (binary). Even though you
may see characters on your screen, internally so, it is store and manipulate as a combination
of 0’s and 1’s.

Then, This conversion of a character to a number is call encoding, and the reverse process is
decoding. Moreover
, ASCII and Unicode are some of the popular encoding used.
So, In Python, the string is a sequence of Unicode characters. After that Unicode was introduce to include every character in all languages and bring uniformity in encoding.

How to create a string in Python?

So, Strings can be created by enclosing characters inside a single quote or double quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and docstrings.

all of the following are equivalent
my_string = 'Hi'
my_string = "Hello"
my_string = '''Hay'''
triple quotes string can extend multiple
my_string = """Hello, welcome to
the world of Python"""
Hello, welcome to
the world of Python
  1. So, Strings can be output to the screen using the print function. For example print(“hello”).
  2. Moreover, Like many other popular programming languages, strings in Python are arrays of bytes representing Unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of Square brackets can be used to access elements of the string.

How to access characters in a String in python?

  1. So, We can access individual characters using indexing and a range of characters using
  2. Moreover, Index starts from 0.
  3. Then, After Trying to access a character out of the index range will raise an Index Error.
  4. The index must be an integer.
  5. Then, We can’t use float or other types, this will result in TypeError.
  6. So, Python allows negative indexing for its sequences.
  7. The index of -1 refers to the last item, -2 to the second last item, and so on.
  8. Then, We can access a range of items in a string by using the slicing operator (colon).
  9. At last, Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods.

Go for online python compiler and you can compile it

str = 'programiz'
print('str = ', str)
first character
print('str[0] = ', str[0])
last character
print('str[-1] = ', str[-1])
slicing 2nd to 5th character
print('str[1:5] = ', str[1:5])
slicing 6th to 2nd last character
print('str[5:-2] = ', str[5:-2])
str = programiz
str[0] = p
str[-1] = z
str[1:5] = rogr
str[5:-2] = am
# index must be in range
>>> my_string[15] 
... IndexError: string index out of range
# index must be an integer
>>> my_string[1.5] 
... TypeError: string indices must be integers

So, Slicing can be best visualized by considering the index to be between the elements as shown below. Then If we want to access a range, we need the index that will slice the portion from the string.

String in python

How to change or delete a string?

So, Strings are immutable. This means that elements of a string cannot be changed once it has been assigned. Then, We can simply reassign different strings to the same name.

>>> my_string = 'programiz'
>>> my_string[5] = 'a'
... TypeError: 'str' object does not support item assignment
>>> my_string = 'Python'>>> my_string

So, We cannot delete or remove characters from a string. But deleting the string entirely is possible using the keyword del.

>>> del my_string[1]
... TypeError: 'str' object doesn't support item deletion
>>> del my_string
>>> my_string
... NameError: name 'my_string' is not defined

So, thank you for reading thing article. In next we have to discuss different types of string in python.

Python Programming Archives – Learning Points

also for more studies visit our website- learning points – Home | A better learning future starts here

By Tanmay

Leave a Reply

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