Join Now
Quality Rating:
  • Currently 2.5 / 5
(2.5 / 5 - 2 votes cast)
Expertise Level:
  • Currently 2.0 / 5
(2.0 / 5 - 2 votes cast)

This page was last modified 18:41, 14 July 2008.

String Handling In Python

From Forum Nokia Wiki


Contents

Introduction

When we are talking about programming be it any kind of programming strings play an important role in that. In programming it is very important when we enter something in textual form or in any other form we have to convert that to form that is easily understandable programmatically. Here strings play an very important role. In python its very easy to manipulate and handle strings. Strings in python are objects also so we don't need to import any module for that strings are easily accessed by the dot operator which will be explained following . This article will explain some of the methods to handle or manipulate strings.

How to Define a String

Defining a string is very easy in Python. We can define strings in many ways as explained below:

nam = "gargi"
nam = 'gargi'

The first way is the most common way i.e. to define a string using double quotes. The second way is also simple we can define strings with use of single quotes. strings can easily be defined using both double and single quotes.

Accessing Parts of String

Strings are so special because we can easily access any character of the string using the [] brackets which specify the index. If we want to find the starting index of any character we can use the find() function. Below are explained some good examples

wrd = "forum nokia wiki is great"
print wrd[9]
print wrd.find("wiki")
print wrd[wrd.find("wiki")]

As explained above the first statement defines a string. Second one will print k. Third statement will return 13 and fourth will print w.

Decision making using strings

pass = "abc"
if len(pass)< 8;
   print access denied

As shown in the code above, decision making is very simple using strings in Python. We can directly compare a string with something and make our decision based on that. Otherwise as shown in the above code we can calculate the length of a particular string and based on that we can take decisions. We can also use the find() to make decisions.

Formating Strings

The main function of strings lie on formating a particular statement. We can put strings which are actually based on some other variables which can be integers or any other data types. To insert a string in middle of a string we have to use the % operator as shown below.

The value of the string to be placed will be inside a tuple. It will be very clear from examples following.

print " forum nokia is %d for %d"("great" , "developers") 
 txt = [5000, "wiki"]
 print "there are now more than %s articles in %d" (txt)
Related Discussions
Thread Thread Starter Forum Replies Last Post
Only one Python HelloWorld App Pleeeeeeaaase shangab Python 3 2005-03-31 10:58
Random Number Generating Roadphantom13 Python 1 2007-12-07 06:50
Database in Python S60 Faisal Rehman Python 14 2008-07-31 17:29
Socket-server in Python rexwal Python 4 2008-02-08 00:36
Install Python 3.0a5 on Windows Vista System Hugh Pittman Python 5 2008-06-09 11:17
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtopicQUqfnTopicZpythonQ
     qfnZtopicQUqfnTopicZseriesE5f60Q
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX