Operation on string in Python

In this article, we will learn how to perform operation on string in Python with example. It is the continuation seventh article of "Zero To Hero In Python" seriesEverything we will understand step by step. Before read this article go through my previous articles. 





Let's start, 

String: Strings in an array of bytes which represent Unicode characters in python. Python does not support character datatype. A single character is also work as string.
Python support to write string within single quote and double quote.

Example: "Python" or 'Python'

Code:

SingleQuotes ='Python in Single Quotes'
DoubleQuotes ="Python in Double Quotes"
print(SingleQuotes)
print(DoubleQuotes)

Output:



In the above code string values assign within variable using single and double quotes.

Output:









  

String can be output to screen, using the print function(print()).
Example: print("hello")

Like many other popular programming languages, string in Python are array of bytes presenting Unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets
can be used to access elements of the string.

1. Indexing in String: it is return particular character from given string.

Syntax:  getChar = a[index]

Example:


message = "Hello, Python!"


If I want to fetch 7th index character from given above string.

Print(string[7])

Output:



Python provide the facility to positive with negative indexing also. 

Example: 


#Text = P Y T H O N
# Positive Index = 0 1 2 3 4 5
# Negative Index = -(6 5 4 3 2 1)

In above code python allow us to get get particular character using positive and negative indexing.

Positive Index: 


#Text = P Y T H O N
# Positive Index = 0 1 2 3 4 5
# Negative Index = -(6 5 4 3 2 1)
Text = 'PYTHON'
print(Text[3])

Output:








you can see in our positive index code index 3 contains H and our output is also H.

  Negative Index: 



#Text = P Y T H O N
# Positive Index = 0 1 2 3 4 5
# Negative Index = -(6 5 4 3 2 1)
Text = 'PYTHON'
print(Text[-4])


Output: 




Now you can see in above code negative index of PYTHON, N (-1), O (-2), H (-3), T (-4), Y (-5), P (-6). When we will call Text(-4) then it will print which you can it in output.

2. Substring: Get substring from given string use indexing with colon. 

Syntax: string[3:5]

Example:

message = msg_string(“Hello Python”)








Some functions which is used with string in function. They are given below.

3. Strip(): This function is used to remove white spaces from given string.

Syntax: stringVariable.strip()

Example:



message ="    Welcome Python.  "
print (message.strip())


Output:





4. lower(): This function is used for return string in lower case.

Syntax: stringVariable.lower()

Example :



message ="Welcome Python!"
print (message.lower())


Output:



5. upper():  This function is used for return string in upper case.

Syntax: stringVariable.upper()

Example:


message ="Python tutorial with dotnettechpoint.com"
print (message.upper())


Output:


6. len(): This function returns the length of given string.

Syntaxlen(stringVariable)

Example


message ="Welcome Python!"
print (len(message))






Output: 




7. replace(): This function is used to replace a string with another string.

Syntax: stringVariable("replaceString","withnewString")

Example:


message ="Python with dotnettechpoint.com"
print (message.replace("Python","Learn Python"))


Output: 





 

8. split(): This function splits the string into substring if any separator is available:

Syntax : string_variable.split("seperator")

Example:



message ="Python, tutorials, with, dotnettechpoint.com"
print (message.split(","))


Output:






9. title(): This function returns fist letter of each word in upper case.

Syntax: stringVeriable.title()



message ="python tutorials with dotnettechpoint.com"
print (message.title())


 In the above code first letter in each word is lower case :

Output:




10. capitalize(): This function is used for covert first character in upper case.

Syntax: variable.capitalize()

Example:



message ="python tutorials with dotnettechpoint.com"
print (message.capitalize())


Output:











11. Count (): This function returns occurrence of subsisting in a string.

Syntax: variable.count(‘string’)

Example:



message ="python tutorials with dotnettechpoint.com"
print (message.count('t'))


Output:







12. Find(): This function returns which index your search text is available in given string.

Note:
1. If find() function returns -1 then search text is not found.
2. The find() method is same as the index() method, the only difference is that the index() method raises an exception if the value is not found. 



Syntax:  stringVariable.find(“searchText”)

Example:


message ="Python tutorials with dotnettechpoint.com"
print (message.find('with'))


Output:










13. Join():This function append particular things in given all items.

Syntax: print(“-”.join (“Python Tutorials”)

Example:



message ="Python Tutorials"
print(" ".join(message))


Output:


Concatenation of strings: We can concatenate two or more than two string in python using + operator but we can not concatenate string with another datatype, when we will try to concatenate then it, It throw an error. 


first_string ="Python"
last_string = "Tutorial"
print (first_string +" " +last_string)


Output :





Concatenate string with another datatype let's see what happen

Example:


first_string ="Python "
middle_string =3
last_string = "Tutorial"
print (first_string +" "+middle_string+" " +last_string)


Output:







In the above output error clearly mentioned that concatenate only string not int, now what is the solution of this problem we can solve this problem to convert int into string or pass middle_string as string and then concatenate it. Try to both way.

Solution 1:


first_string ="Python "
middle_string ="3"
last_string = "Tutorial"
print (first_string +" "+middle_string+" " +last_string)


Output:



Solution 2:


first_string ="Python "
middle_string =3
last_string = "Tutorial"
print (first_string +" "+str(middle_string)+" " +last_string)


Output:




















No comments

Powered by Blogger.