From this text I’m going to talk about numbers and strings with some of the methods used with these Data Types.
- We can simply use numbers in mathematical calculations directly. ‘**‘ is used to get the power of a number.
- Also we can use numbers with floating points as well.
- We can use the str() to convert a number to a string.
- len() is used to find the length of a string.
So if we want to get the length of a number, first of all we have to convert it into a string and then find the length of it.
- Also there are some built-in mathematical functions as well. We have to import them using the ‘import‘ command before we use them. math class have all the maths methods like pi, sqrt and etc. Random is used to generate a random number.
- Also we can combine methods together.
Now let’s look at the string method in python.
- Just give the value inside ” to make it a string. len() method can be used with string as we talked earlier.
- Also we can use variables to store strings and then using that variable name we can get specific locations value like following.
- Also we can slice strings as well.
- With numbers we used ‘+‘ as a mathematical operator, but with strings we can use it as a string concatenation operator. Also ‘*‘ is used to repeat strings.
- find() is used to find a specific part of a string. If it found the search value then it will output the indexed value where it found the that searched part.
- replace() can be used to search and replace a part of a string.
- upper() is used to convert a string to uppercase and lower() to convert a string to all lowercase.
- Also we can use isalpha() and isdigit() for identify the value type.
- split() is used to trim a string from a specific location.
- rstrip() is used to remove the extra white spaces from th right hand side of the string.
- Also there are some string formatting techniques as well.
- There are two special commands used in python where we can get information about methods and available methods for an object type.
I tried to give a basic knowledge about some useful methods used with numbers and strings in python. Hope you got an idea about them. See you soon with the rest of the object types used in python. Thank You!