Is it possible to determine if a string begins with an character repeating x times?

349
December 15, 2016, at 12:44 PM

I have a two strings

code_one = "222abc"
code_two = "2abc"

Is there a way I can determine that strings begin with "2" repeating any number of times?

Answer 1

You can simply use lstrip() and compare the lengths:

>>> code_one = "222abc"
>>> len(code_one) - len(code_one.lstrip("2"))
3

Or if you just want to check the string starts with some characters:

>>> code_one.startswith("222")
True
READ ALSO
How to add whitespace after string.punctuation in Python?

How to add whitespace after string.punctuation in Python?

I want to clean my reviews dataHere's my code :

497
get the column from 2d array to calculate the normalization and cross product in python

get the column from 2d array to calculate the normalization and cross product in python

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on)

550
How to handle processing of large file on GAE?

How to handle processing of large file on GAE?

I'm looking for a powerful and fast way to handle processing of large file in Google App Engine

330
Density of multivariate t distribution in Python for large number of observations

Density of multivariate t distribution in Python for large number of observations

I am trying to evaluate the density of multivariate t distribution of a 13-d vectorUsing the dmvt function from the mvtnorm package in R, the result I get is

342