Blog

Python Strings
9 Jan 2020

Python Strings

Python Strings Handling:  A String refers to a collection of characters. In Python, a string value can be mentioned either within “ “ (double Quote Symbol) or the value can be enclosed within ‘ ‘(Single Quote Symbol). Let us try to explain with an example –

Mystring=” Hello This is my First Program”

In the above statement, Mystring is a variable containing string value – Hello This is my First Program. Sometimes if required an empty String variable can also be formed as follows –

String1=””

In Python to deal with string values, some special operators are also used – the following table will show the operators and their usage.

OperatorDescriptionExample[ Assume a=“Hello”, b=“Python”]
+Concatenation – Adds values on either side of the operatora + b will produce HelloPython
*Repetition – Produces new strings, concatenating multiple copies of the same stringa*2 will give –HelloHello
[]Slice – Extracts the character from the given indexa[1] will give e
[ : ]Range Slice – Extracts the characters from the given rangea[1:4] will give ell
inMembership – Returns True if a character exists in the given string otherwise False value will be returned.H in a will give 1
not in Membership – It will return True if a character does not exist in the given string- otherwise False value will be returned.M not in a will give 1

 

Let us build some small programs using these operators.

Example 1:

a1=”Hello”

b1=”Python Programmer”

print(a1+” “+b1)

Output

Python Strings

So + is used to concatenate a string with another string. Here is another example and the output is also given below.

a1=”Hello”

b1=”Python Programmer”

print(a1+” “+b1)

print(“=”*20)

Here * symbol works as an operator which is responsible for the printing of = sign 20 times. So in this way, the operators mentioned within the table can be used. Apart from these, there are a lot of many functions that can be applied to string values and they are specially termed as string functions. Shortly we will upload the string functions and their working in the blog.

String Functions in Python

In this episode, we shall try to explore various String Functions and their usage. Usages will be explained by citing some examples.

So let us see the following table which lists some important String Functions.

String Functions

split(): Python string split() function is used to split a string into the list of strings based on a delimiter.

join(): This function returns a new string that is the concatenation of the strings in iterable with string object as a delimiter.

strip(): Used to trim whitespaces or blank characters from a given String.

format_map(): In Python format_map() function returns a formatted version of the string using substitutions from the mapping provided.

upper(): We can convert a string to uppercase in Python using the upper() function.

ower(): This function creates a new string in lowercase.

title(): Converts a String into Title Case means each first character of each Word is Capital and rest will be in small case characters.

replace(): In Python, replace() function is used to create a new string by replacing some parts of another string.

find(): In Python find() method is used to find the index of a substring in a string.

translate(): Python String translate() function returns a new string with each character in the string replaced using the given translation table.

Now let us Do Some Programs using the above functions

 

Example 1: Converting a String into Upper Case

myname=input(“Enter Your Sentence”)
print(“Message = ” +myname.upper())

So in the above program if you enter the String as – Python Programming the output will be displayed as –

Message = PYTHON PROGRAMMING

 

Example 2: Counting No. of Words from a Given String

mysentence=input(“Enter the String”)
wordsstorage=mysentence.split(” “)
print(“Total Words = %d” %(len(wordsstorage)))
print(wordsstorage)

So in this program, if you enter the String as “All India Radio” the output will be shown as –
Total Words = 3

Point to be noted is that in this program without using loop we have counted no. of words from the given string. The split() function actually separates each word basis on spaces in between the words. All the words are stored in a list which is wordstorage here. Using the len(), which is an inbuilt function we can count the no. of items from the woodstorage variable and thus we are able to determine the no. of words from the string variable.

Sometimes we can use various operators on String values like + is used as a concatenation operator- an example will be sufficient to understand its working.

String1=”Innovative”
String2=”Technology”
String3=”Solutions”
String4= String1+” “+String2+” “+String3
print(String4)

The Output will be – Innovative Technology Solutions