Is there a build-in function of Python to get the number of a string?
I have a type of string like this '20180318_beta'
.
I want to get the 20180318
of it, so is there a build-in method for achieving this?
Is there a build-in function of Python to get the number of a string?
I have a type of string like this '20180318_beta'
.
I want to get the 20180318
of it, so is there a build-in method for achieving this?
Here is a list of built in functions: https://docs.python.org/3/library/functions.html
As you can see, there is no specific function for what you are asking. However, if you wanted to build a function for it, there are many questions you have to ask yourself. Can it handle negatives? Can you find non-integers? What about numbers in different places?
You can always use regex to attempt this problem. Here is a previous post about it: Python: Extract numbers from a string
If all of your cases will be similar to the one above, you can always use the split method. In other cases, I would refer to this post: Extract Number from String in Python
Just as a side note, if you do use the last link, be careful with the top answer with Python 3.x as the filter function is slightly different than its counterpart.
Well depending on the robustness you require, casting to int will work in this case because your string starts with numbers. However, If it started with a letter it would fail.
It's pretty simple to roll your own, see this stack overflow answer.