Python String

If you want to learn python then string is the necessary things to know .Here in this article I will explain all things that are mainly important to know when you are learning string.

Definition of string:

  • String is a sequence of characters, anything inside single, double and triple quote is called as string in python.
  • Remember String is immutable. You can’t modify the string you declare.
  • In Python strings are Unicode characters.
  • There is encoding and decoding of characters to use all languages.

Example:

  • ‘PyGround’
  • “PyGround”
  • ”’PyGround”’

How to declare a string ?:

  • my_str = ‘PyGround’
  • my_str1 = “PyGround”
  • my_str2 = ”’PyGround”’
String Operations:
  1. Create a string
  2. Accessing elements from the string.
  3. Change or delete a string
  4. String Methods

1.Create a string:

In above you know the definition of string. So lets some example to understand more.

Example:

>>> my_str = ‘Pyground’
>>> print (my_str)
Pyground
>>> my_str = “Pyground”
>>> print (my_str)
Pyground

>>> my_str = ”’Pyground”’
>>> print (my_str)
Pyground
>>>

Explanation:

Here we used all the format of string declaration, which is so simple to write .

2.Accessing elements from the string.
  • Accessing characters from a string , we can use indexing or slicing to access each character from the string.
  • Index start from 0 and we can use -1 to traverse string from last character.
  • Visualize the indexing here. which is similar to list. 

String representation inside memory

P Y G R O U N D
0 1 2 3 4 5 6 7
-8 -7 -6 -5 -4 -3 -2 -1

Lets do some practical to get more details.

Example:

> my_str = “Pyground”
>>> first_char = my_str[0]
>>> print (‘first_char:’,first_char)
first_char: P
>>> last_char = my_str[-1]

>> print (‘last_char:’,last_char)
last_char: d
>>> slice_char = my_str[1:4]
>>> print (‘slice_char:’,slice_char)
slice_char: ygr
>>>

 

Explanation:

  • Here we used index o for the first element and index -1 to get the last element.
  • When we used slicing [1:4] which start from 1st till the 4th index , excluding 4th index element.
  • If you want to traverse  from last then the index start from -1.

3.Change or delete a string
  • We can not change any string once it is assigned.
  • Strings are immutable.
  • But we can declare another string with the same name.

Example:

>> my_str = “PyGround”
>>> my_str[2] = ‘ty’
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: ‘str’ object does not support item assignment
>>>

Explanation:

  • You can’t change a string element.

If you want to delete the entire string, then you can do it by using “del” Keyword.

Example:

>> my_str = “PyGround”
>>> del my_str
>>> print (my_str)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
NameError: name ‘my_str’ is not defined
>>>

Adding two String in python:

  • We can add two string like two numbers
  • Concatenation of two string.

Example:

>> str1 = “Py”
>>> str2 = “Ground”
>>> str1+str2
‘PyGround’
>>>

Explanation:

  • Just add the two string like you use addition operator.
  • That will create a new string.

Multiply two String in python:

  • Just like add we can use * to repeat the string multiple times.

Example:

>> my_str = “PyGround”
>>> my_str*2
‘PyGroundPyGround’
>>>

Explanation:

  • When you use multiplication operator it will repeat that string in that much time.
  • Here we used 2 so the string repeated two times and create a new string.

String Methods:

Below are some methods used in string formatting.

  • lower()
  • upper()
  • Join()
  • split()
  • find()
  • replace()

Example:

>> my_str = ‘PyGround’
>>> my_str.lower()
‘pyground’
>>> my_str.upper()
‘PYGROUND’
>>>

Split() method  

  • If You split a string then it will divided a string with a list of elements.

Example:

>> my_str = “PyGround center”
>>> my_str.split()
[‘PyGround’, ‘center’]
>>>

Explanation:

  • Here you saw the string is divided  with two elements.
  • we are not using any argument inside split that means it split with the spaces by default.

Example:

>> my_str = ‘PyGround##center’
>>> my_str.split(‘##’)
[‘PyGround’, ‘center’]
>>>

Explanation:

  • Above it split with ‘##’ and created a list out of this.

Join() method

  • It joins  strings inside a list , you will understand by looking below examples.

 

Example:

> my_str = “PyGround center”
>>> my_str.split()
[‘PyGround’, ‘center’]
>>> data = my_str.split()
>>> ‘ ‘.join(data)
‘PyGround center’
>>>

Explanation:

  • Here it joined two string with a space in between.

Example:

> my_str = ‘PyGround##center’
>>> data = my_str.split(‘##’)
>>> print (data)
[‘PyGround’, ‘center’]
>>> ‘##’.join(data)
‘PyGround##center’
>>>

Explanation:

  • Here it joined two element with ‘##’ and create a new string.

find() method

Example:

>> my_str = “Learn Python from PyGround”
>>> my_str.find(‘Python’)
6
>>> my_str.find(‘data’)
-1
>>>

 

Explanation:

  • Here the string ‘Python’ is present and it returns the index of that  string from where it start..
  • Then we searched for ‘data’ which is not present and that return us -1.

replace() method:

Example:

>> my_str = “Learn Python from PyGround”
>>> my_str.replace(‘Learn’,’Read’)
‘Read Python from PyGround’
>>>

Explanation;

  • Replace method replace the source string with required string
  • Here it replace ‘Read’ with ”Learn’.

Now you understood all about string. If you liked this post then share and comments below.

Be a master in python by joining our Python crash course.

Share this post with your friends and colleges who wants to learn python and grow there career.

9 thoughts on “Python String”

  1. I’m always looking for web sites that sell real bags. I’ve purchased several bags over the last year. or the bags are always perfect, the shipping time is very prompt, and the communication is excellent. including the I highly recommend this site to anyone who’s looking for real bags. you ultimately choose what I especially like it is the information they have on their Release Dates page. This really helps me stay in tune with what’s going on in the world of bags.
    louis vuittons outlet https://www.louisvuittonsoutlet.com/

  2. I received my jordans yesterday. This is the first time ordering shoes from this website and I am definitely satisfied with them. or simply I would give this website a 20 on a scale from 1-10 and would certainly recommend this site to all. exactly like the I will definitely be purchasing more items in the future. manner in which It has been a pleasure.
    cheap jordan shoes https://www.realcheapjordan.com/

  3. I received my jordans yesterday. This is the first time ordering shoes from this website and I am definitely satisfied with them. on the other hand I would give this website a 20 on a scale from 1-10 and would certainly recommend this site to all. like the I will definitely be purchasing more items in the future. in either case It has been a pleasure.
    cheap real jordans https://www.jordanb2c.com/

  4. I received my jordans yesterday. This is the first time ordering shoes from this website and I am definitely satisfied with them. or sometimes I would give this website a 20 on a scale from 1-10 and would certainly recommend this site to all. which includes the I will definitely be purchasing more items in the future. direction It has been a pleasure.
    cheap jordans https://www.cheapretrojordan.com/

  5. After reading your article, it reminded me of some things about gate io that I studied before. The content is similar to yours, but your thinking is very special, which gave me a different idea. Thank you. But I still have some questions I want to ask you, I will always pay attention. Thanks.