Start To Learn Python Right Now EP05: Python Strings & Basics

In Episode 04 we talked about the Python Variables, Datatypes, and how to use them. So, in this post, I thought to explain Python strings (str). Normally strings are the text in Python. we can use them anywhere in our projects. These are some examples of it, 

               1. When you need to print a text 

               2. When you want to get a text input 

               3. When you want to get a special Character from a text

    There are more, these are only a few from the usage of Python Strings. Now let's see how to make different String types in Python.

 'Hello, www.softexs.com

"Hello, www.softexs.com

     This is is how to make String data in Python. There is no " " or ' ' difference when making a sting but you can have a problem when making a string like this 'softexs.com's website'.  in this String, we are using another single quote ( ' )inside the string.so Python this is as an incomplete string. so, you can avoid that kind of error using ("softexs.com's website") single quotes( ' ) inside the double quotes( " ).

This is the Problem;


This is how to solve it;

Using Multiple lines inside a string 

     We talk about how we are using print() statements to display a value in Python. Also, you can make a variable that is assigned to a string. If you don't have good knowledge about variables, you can refer the Episode 04. But how you use few lines in your stings. because " " or ' ' quotes we talked are not supporting to multiple lines, Well you can use this method to avoid that problem.

  '''Hello, I'm softexs.com. I'm a science
   and technology News website. You can 
   also learn programming n this website'''

You can use (''' ''') triple quotes to write multiple lines in Python.

This is the Problem;


This is how to solve it;

How to get characters from String

Now let's see how to get a character from a string. Now, we have the word "softexs", So I need to get the word "s" from it, but now I do that using Python ?. Well, in Python strings work as a list so you can get any word from the string like this,
          a = "softexs"
         print(a[0])
 Now, you may have a problem, with why I used '0'. because Python values started in '0'. Also, you can get the characters from the end using '-1'. It is like this;
         a = "softexs"
         print(a[-1])

Now let's learn to get a character range from a word. but you need to know this only works on the length of the word. I mean the character count. You can get word length by using this simple code)
  a = "softexs"
  print(len(a))

You can use the below code example to get the character range from Python String. You can learn more about it from the Python code editor below
    a = softexs
    print(a[0:5])

Python Strings lesson is a bit long lesson, Let's learn about formatting and uppercase lower case in the next post.

Post a Comment

If you have any doubt, Let us know

Previous Post Next Post