Round 3 of strings in Python

Few methods of working with strings:String Methods

Python is an object-oriented language as we know, so every data and functions are also functions. Data types such as Strings, list and others have function which help to manipulate the data types, make work easier, we just refer these functions are string methods. So, Methods are just like functions, but they are always attached to a value . Lets take an example to make it clear, all string values have append() method which appends the data to the object.

Syntax: object.method( )

which means that the method associated with the object’s type is applied to the object. This is just a special syntax for a function call with an object.

Before we dig into methods we need some more knowledge on Lists, we did discuss on it in last blog.

A List is a sequence which can contain several other values in it. Consider this example:

[python]
>>> ['PyThOn',"Mr.Spark",1234]
['PyThOn', 'Mr.Spark', 1234]
[/python]

This is a List with 3 data(which includes strings, numbers). Like other variables we can store this in a variable.

[python]
>>> new=['Enter','World of python',"Linux candy"]
>>> new
['Enter', 'World of python', 'Linux candy']
[/python]

likewise we can access each with the index values:

[python]
>>> new[1]
‘World of python’
[/python]

List can be concatenated also,

[python]
>>> [1,2,3,4]+['one','two','three','four']
[1, 2, 3, 4, 'one', 'two', 'three', 'four']
>>> new+[1,2,3]
['Enter', 'World of python', 'Linux candy', 1, 2, 3]
[/python]

In Operator: To check whether there is a specific value in the List, we can use in operator. The return type will be in Boolean value(True or False).

Syntax: value in list_name

[python]
>>> languages=['python','c#','ROR']
>>> ‘c#’ in languages
True
>>> v=[1,2,'3']
>>> 1 in v
True
[/python]

Removing items from list using del operator

We know that we can access the values in the list using index values, likewise, its also possible to delete a value in the list, using del operator.

[python]
>>> x=[1,2,3,'hello','world']
>>> del x[1]
>>> x
[1, 3, 'hello', 'world']
>>> del x[3]
>>> x
[1, 3, 'hello']
[/python]

We must remember that del is a statement not a operator, which evaluates but does not return any value.

Lets get back to String Methods,

→ The Reverse() list methods:

The function of reverse() method is that it reverses the order of items in the list.

Syntax: list_name.reverse()

[python]
>>> var=[1,2,3,4,5,6,7]
>>> var.reverse()
>>> var
[7, 6, 5, 4, 3, 2, 1]
[/python]

→ The append() list method:

The append() method is used to add the value to end the of the List.

Syntax: List_name.append(value)

[python]
>>> animal=['lion','tiger','cheetah','zebra']
>>> animal.append(‘gorilla’)
>>> animal
['lion', 'tiger', 'cheetah', 'zebra', 'gorilla']
>>>animal.append(1)
>>> animal
['lion', 'tiger', 'cheetah', 'zebra', 'gorilla', 1]
[/python]

Few methods related Strings: lower() and upper() string methods

→ The lower() string methods

It changes all characters in the strings into lowercase characters.

Syntax: string_variable.lower()

[python]
>>> greet=’HellO WoRlD!! 133t’
>>> greet.lower()
‘hello world!! 133t’
[/python]

→ The upper() string methods

It changes all characters in the strings into uppercase characters.

Syntax: string_variable.upper()

[python]
>>> greet.upper()
‘HELLO WORLD!! 133T’
[/python]

count() method:

It counts the number of appearances of a specific item in the string.

Syntax: String_variable.count(‘value’)

[python]
>>> var=’Mississipi’
>>> var.count(‘s’)
4
[/python]

Few more String methods are:

  • Case manipulation (capitalize, upper, lower, swapcase, title)
  • Counting (count—number of times substring is in string)
  • Manipulating text encoding (encode, decode)
  • Search and replace (find, replace, rfind, index, rindex, translate)
  • Tests that return Boolean (True or False) values (startswith, endswith, isalnum, isalpha, isdigit, islower, isspace, istitle, isupper)
  • Joining and splitting (join, partition, rpartition, split, splitlines)
  • Formatting (center, ljust, lstrip, rstring, rjust, strip, zfill, expandtabs)

Few methods of working with strings:String Methods

Python is an object-oriented language as we know, so every data and functions are also functions. Data types such as Strings, list and others have function which help to manipulate the data types, make work easier, we just refer these functions are string methods. So, Methods are just like functions, but they are always attached to a value . Lets take an example to make it clear, all string values have append() method which appends the data to the object.

Syntax: object.method( )

which means that the method associated with the object’s type is applied to the object. This is just a special syntax for a function call with an object.

Before we dig into methods we need some more knowledge on Lists, we did discuss on it in last blog.

A List is a sequence which can contain several other values in it. Consider this example:

[python]
>>> ['PyThOn',"Mr.Spark",1234]
['PyThOn', 'Mr.Spark', 1234]
[/python]

This is a List with 3 data(which includes strings, numbers). Like other variables we can store this in a variable.

[python]
>>> new=['Enter','World of python',"Linux candy"]
>>> new
['Enter', 'World of python', 'Linux candy']
[/python]

likewise we can access each with the index values:

[python]
>>> new[1]
‘World of python’
[/python]

List can be concatenated also,

[python]
>>> [1,2,3,4]+['one','two','three','four']
[1, 2, 3, 4, 'one', 'two', 'three', 'four']
>>> new+[1,2,3]
['Enter', 'World of python', 'Linux candy', 1, 2, 3]
[/python]

In Operator: To check whether there is a specific value in the List, we can use in operator. The return type will be in Boolean value(True or False).

Syntax: value in list_name

[python]
>>> languages=['python','c#','ROR']
>>> ‘c#’ in languages
True
>>> v=[1,2,'3']
>>> 1 in v
True
[/python]

Removing items from list using del operator

We know that we can access the values in the list using index values, likewise, its also possible to delete a value in the list, using del operator.

[python]
>>> x=[1,2,3,'hello','world']
>>> del x[1]
>>> x
[1, 3, 'hello', 'world']
>>> del x[3]
>>> x
[1, 3, 'hello']
[/python]

We must remember that del is a statement not a operator, which evaluates but does not return any value.

Lets get back to String Methods,

→ The Reverse() list methods:

The function of reverse() method is that it reverses the order of items in the list.

Syntax: list_name.reverse()

[python]
>>> var=[1,2,3,4,5,6,7]
>>> var.reverse()
>>> var
[7, 6, 5, 4, 3, 2, 1]
[/python]

→ The append() list method:

The append() method is used to add the value to end the of the List.

Syntax: List_name.append(value)

[python]
>>> animal=['lion','tiger','cheetah','zebra']
>>> animal.append(‘gorilla’)
>>> animal
['lion', 'tiger', 'cheetah', 'zebra', 'gorilla']
>>>animal.append(1)
>>> animal
['lion', 'tiger', 'cheetah', 'zebra', 'gorilla', 1]
[/python]

Few methods related Strings: lower() and upper() string methods

→ The lower() string methods

It changes all characters in the strings into lowercase characters.

Syntax: string_variable.lower()

[python]
>>> greet=’HellO WoRlD!! 133t’
>>> greet.lower()
‘hello world!! 133t’
[/python]

→ The upper() string methods

It changes all characters in the strings into uppercase characters.

Syntax: string_variable.upper()

[python]
>>> greet.upper()
‘HELLO WORLD!! 133T’
[/python]

count() method:

It counts the number of appearances of a specific item in the string.

Syntax: String_variable.count(‘value’)

[python]
>>> var=’Mississipi’
>>> var.count(‘s’)
4
[/python]

Few more String methods are:

  • Case manipulation (capitalize, upper, lower, swapcase, title)
  • Counting (count—number of times substring is in string)
  • Manipulating text encoding (encode, decode)
  • Search and replace (find, replace, rfind, index, rindex, translate)
  • Tests that return Boolean (True or False) values (startswith, endswith, isalnum, isalpha, isdigit, islower, isspace, istitle, isupper)
  • Joining and splitting (join, partition, rpartition, split, splitlines)
  • Formatting (center, ljust, lstrip, rstring, rjust, strip, zfill, expandtabs)

In the next Blog, we will jump onto Functions, if statements and other condition. In that blog will include a Hangman game also.

Advertisement