- The string method lower will return the string with all characters changed to lowercase. The inverse of the upper method, which will return the string 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 alsostring”. be super handy when checking user input since someone might type lowercase or even of cases.
- We can use the strip method surrounding white space from a string. White space includes spaces, tabs, and newline characters.
- We can also use the methods Istrip and rstrip white space only from the left or side of the string, respectively.
- The method count return of times as substring appears in a string. This can be handy out characters appear string or counting of times word appears sentence or paragraph.
- If we wanted if a string ends with a given substring, you can use with. This will return True if the substring is found at of the string, and False if not.
- This is numeric method can check if a string of only numbers. If the string contains numbers before passing the string to the int() function to convert it to an integer, avoiding .Useful!
- We took at string concatenation using the , earlier. We can also use the join method to concatenate strings. This method takes of strings to be joined as a parameter and returns string composed of the strings from our list joined using the initial string.For example,”“.join([This”,”is”,”a”,”sentence”]) would return the string “This sentence”.
- The inverse of the join method split method. This allows us a string into 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'> >>>
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 >>>
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