Uncategorized

4. Strings

Syllabus :

Manipulating Strings: Working with Strings, Useful String Methods, Format operators , Project: Password Locker,
Project: Adding Bullets to Wiki Markup

4.1 Working with Strings :

A string is a sequence of characters. It can be created by enclosing characters in single or double or triple quotes.

Example : ‘Hello World!’ , “Python Programming“ ,   ”’apple”’

Creating a string :

Creating a string is as simple as assigning a value to variables as illustrated below :

 var1 = 'Hello World!' 
 var2 = "Python Programming"
 var3 = '''Water Melon'''

Triple quotes can extend multiple lines as illustrated below :

Length of a string

One can get last letter of string using len( ) function as illustrated below :

String Indexing

In Python string can be indexed in forward and backward direction as illustrated in the figure below :

Consider a string name = ” Sophia“. Here string Sophia is made up of 6 characters. Each character of the string can be accessed by using either forward indexing or backward indexing as illustrated below:

String Traversal

String traversal can be performed Using looping statements like for loop and while loop .

String traversal using while loop

String traversal using for loop

String traversal in reverse order

Lists aren’t the only data types that represent ordered sequences of values.For example, strings and lists are actually similar, if you consider a string to be a “list” of single text characters. Many of the things you can do with lists  can also be done with strings: indexing; slicing; and using them with for loops, with len(), and with the in and not in operators.