EASY STEPS TO LEARN STRING DATATYPE IN PYTHON – 3

string datatype Innovate Yourself

OVERVIEW

In this post, you will learn,

  • What are string datatype?
  • How to use string datatype?
  • Slicing in python
  • Methods available in string datatype

What are strings?

String is a sequence of Unicode characters. These are defined and identified by str class. We can use either single quotes(‘ ‘) or double quotes(” “) to represent strings. We can also use triple quotes, (”’ or “””) to store the multiple line strings.

Here are the examples for the string datatype.

msg="This is a single line string"
print(msg)
msg='''This is a multiple line string.
here we can store the string data in multiple lines.'''
print(msg)
Output: This is a single line string 
This is a multiple line string. 
here we can store the string data in multiple lines.

How to use strings?

Above you have seen few examples, that how you can create a string object that will store the string datatype. Now, it’s time to understand that how to use the string and what other operations that we can perform on it.

To understand it better let’s create two strings that we’ll create with respect to single as well as double quotes and will further apply the arithmetic operators that can be applied on strings.

string1='Innovate'      # Creating a string with single quotes
string2="Yourself"      # Creating a string with double quotes
print(string1,string2)

# Only using the add(+) arithmetic operator with two strings you will get the two strings 
# joined together which is called as concatenation
concatenate=string1+" "+string2        
print("Joined Strings: ",concatenate)

# Now if we will do the multiplication of a string with the integer and float value then you will get
a,b=4,5.5
print(concatenate*a)
print(concatenate*b)
Output:
InnovateYourself
Joined Strings:  Innovate Yourself
Innovate YourselfInnovate YourselfInnovate YourselfInnovate Yourself
Traceback (most recent call last):
  File "test.py", line 22, in 
    print(concatenate*b)
TypeError: can't multiply sequence by non-int of type 'float'

Slicing in python

Suppose we have a complete data set and from that dataset we want to slice some part of it, that process is known as slicing. Now we will see how the slicing is done and what are different ways to do the slicing.

Here are few example to demonstrate that how to slice the data in different ways for slicing single character or single word or some part of the string datatype. We can access the string datatype with the index values which can be positive or negative based on starting or the ending position respectively. Which means if the indexing starts from beginning then the indexing will be positive and starts from 0 and if the indexing starts from the end then the index will be negative and starts from -1 as shown here,

data= "My name is Ashish saini from Innovate Yourself."

# access the "M" from my and "A" from ashish with
print(data[0])   # output will be M
print(data[11])   # output will be A

# access the last value
print(data[-1])

# access the word Ashish from sentence data
print(data[11:17])   # output will be Ashish and here end index will be 17 as the last accepted 
                     # index will be 16
# access Ashish saini with
print(data[11:23]) #output will be Ashish saini

# to access all the alternate positions
print(data[0:-1:2])    # here 0 is the starting index, -1 is the last index and 2 is step difference of the     
                       # current and the next position
Output:
M
A
.
Ashish
Ashish saini
M aei sihsiifo noaeYusl.

Above are the different ways how you can use the slicing to slice the data in one or the other way. Also check out our video on slicing for more clarification,

Q 1: Write a program the print the reverse of the string by using slicing?
text=”Hello”

Answer:

print(text[::-1])

Methods available in String

Above we have seen how to slice the data with the easier steps. Now other than the slicing concept we have various methods available for the string datatype which can be accessed with the string object. Example is shown below that will help you understand the various methods accessible through the class object,

message="Innovate Yourself"

now here is the image showing what are the methods available for the string class,

string datatype in python | Innovate yourself

Now, in the above image you can see that there are various methods which are available and accessible with the class object, which we can call and use. Now we’ll see some of them to understand it better.

# To convert all the string to the upper case 
print(message.upper())

# To convert all the string to the lower case 
print(message.lower())

# To find the index value of any string  
print(message.index("Your"))

# To replace any word or character 
print(message.replace("Innovate", "Innovation"))
print(message.replace("Y","y"))

dynamic_message="hello {}"

# to update the string message dynamically, use the format method
print(dynamic_message.format("Ashish"))
Output:
INNOVATE YOURSELF
innovate yourself
9
Innovation Yourself
Innovate yourself
hello Ashish

Check out our video here for more clarification,

Q 2: For the given string write a program to print all the multiple of 3 in decreasing order(from higher to lower value). 0 should be excluded?
input_text=”0123456789″

Answer:
print(input_text[:1:-3])

Q 3: Write a program to take any number between 1 to 12 as an input and to display the first 3 letters of that month name for the given month data.
month=”janfebmaraprmayjunjulaugsepoctnovdec”
Note: Use slicing to solve the given problem.

Answer:

num = 5
month="janfebmaraprmayjunjulaugsepoctnovdec"
print(month[(num-1)*3:(num-1)*3+3])

Output:
may

So, these were some of the methods which you can use for the string datatype. Similarly, there are various other methods available which you can use for the string class. Now, this is a small task for you to work on. Feel free to leave a comment below if you found any difficulty in understanding anything.

Time to wrap up now. Hope you liked our content on string datatype in python. See you in our next blog, thanks for reading our blog and do leave a comment below to help us improve the content to serve you all of our experience with you. Stay tuned with us for more python programming contents.

Also check out our other playlist Rasa Chatbot, Internet of things, Docker, etc.
Become a member of our social family on youtube here.

One thought on “EASY STEPS TO LEARN STRING DATATYPE IN PYTHON – 3

Leave a Reply