Advanced String Methods of Python Data Types

Advanced String Methods of Python Data Types

Description

  • The string method lower will return the string with all characters changed to lowercase. The inverse of this is often the upper method, which will return the string beat uppercase. Just like with previous methods, we call these on a string using dot notation, like “this is the string”. Upper ().
  • This would return the string “This could also be a string”. this will be super handy when checking user input since someone might type altogether lowercase or even a mix of cases.
  • We can use the strip method to get rid of surrounding white space from a string. White space includes spaces, tabs, and newline characters.
  • We can also use the methods Istrip and rstrip to get rid of white space only from the left or the proper side of the string, respectively.
  • The method count is often wont to return the number of times as substring appears in a string. This can be handy for locating out what percentage characters appear during a string or counting the number of times a particular word appears during a sentence or paragraph.
  • If we wanted to see if a string ends with a given substring, you can use the tactic with. This will return True if the substring is found at the top of the string, and False if not.
  • This is numeric method can check if a string consists of only numbers. If the string contains numbers before passing the string to the int() function to convert it to an integer, avoiding a mistake.Useful!
  • We took a glance at string concatenation using the sign, earlier. We can also use the join method to concatenate strings. This method takes an inventory of strings to be joined as a parameter and returns a replacement string composed of every one of the strings from our list joined using the initial string.For example,”“.join([This”,”is”,”a”,”sentence”]) would return the string “This may be a sentence”.
  • The inverse of the join method is that the split method. This allows us to separate a string into an inventory of strings. By default, it splits by any white space characters. You can also split by any other characters by passing a parameter.

Identity and type

Each object has an identity in the form of an integer and a type. The identity corresponds to the position of the object in the memory.

>>> x = 2
>>> id(x)  # id is unique memory address
3077862864
>>> type(x)
<class 'int'>
>>>

>>> # type of class is 'type'
>>> class Circle(object):
...     pass
...
>>> id(Circle)
3070212412
>>> type(Circle)
<class 'type'>
>>>

>>> # type of builtin-type is 'type'
>>> type(int)
<class 'type'>
>>>

‘is’ driver

This driver is used to compare the identity of two objects,

>>> a = [1, 2, 3]
>>> b = a # b is new name for a

>>> # a and b have same id
>>> id(a)
3070184940
>>> id(b)
3070184940

>>> # since a and b are same, hence if modify b, a also changes
>>> b.append(3)
>>> a
[1, 2, 3, 3]
>>>

>>> # 'is' returns true if 'id' is same
>>> a is b
True
>>> b is a
True
>>>

Comparing objects

Objects can be compared grounded on values, id, and types as shown below,

>>> def compare(a, b):
...     if a is b:
...             print(" objects have same id")
...     if a == b:
...             print(" values of objects are same")
...     if type(a) is type(b):
...             print(" types of objects are same")
...

>>> x=[1, 2, 3]
>>> y=[1, 2, 3]
>>> id(x)
3070183308
>>> id(y)
3070185004
>>> compare(x, y)
 values of objects are same
 types of objects are same


>>> compare(2, 2)
objects have same id
values of objects are same
types of objects are same

>>> compare(2, 3)
types of objects are same

>>> x = 3
>>> y = 3
>>> compare(x, y)
 objects have same id
 values of objects are same
 types of objects are same
>>> id(x)
3077862880
>>> id(y)
3077862880

https://www.technologiesinindustry4.com/2020/10/ethereum-erc20.html

Mansoor Ahmed is Chemical Engineer, web developer, a writer currently living in Pakistan. My interests range from technology to web development. I am also interested in programming, writing, and reading.
Posts created 422

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top