Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dot notation string manipulation

Tags:

python

string

Is there a way to manipulate a string in Python using the following ways?

For any string that is stored in dot notation, for example:

s = "classes.students.grades"

Is there a way to change the string to the following:

"classes.students"

Basically, remove everything up to and including the last period. So "restaurants.spanish.food.salty" would become "restaurants.spanish.food".

Additionally, is there any way to identify what comes after the last period? The reason I want to do this is I want to use isDigit().

So, if it was classes.students.grades.0 could I grab the 0 somehow, so I could use an if statement with isdigit, and say if the part of the string after the last period (so 0 in this case) is a digit, remove it, otherwise, leave it.

like image 910
Vandexel Avatar asked Jan 31 '16 19:01

Vandexel


People also ask

How do you write dot notation?

Dot notation is one way to access a property of an object. To use dot notation, write the name of the object, followed by a dot (.), followed by the name of the property. Example: var cat = { name: 'Moo', age: 5, }; console.

What is the function of dot notation?

Dot notation allows you to access specific fields or data nested within a variable. Dot notation is a general programming concept and it is also a useful tool in Xano. You can apply dot notation on any input/value line within the Function Stack or Response - whether that be within Functions or Filters.

What is Dot string in Python?

Strings are objects in Python which means that there is a set of built-in functions that you can use to manipulate strings. You use dot-notation to invoke the functions on a string object such as sentence.

How does dot notation work in Python?

Dot notation indicates that you're accessing data or behaviors for a particular object type. When you use dot notation, you indicate to Python that you want to either run a particular operation on, or to access a particular property of, an object type.


4 Answers

you can use split and join together:

s = "classes.students.grades"
print '.'.join(s.split('.')[:-1])

You are splitting the string on . - it'll give you a list of strings, after that you are joining the list elements back to string separating them by .

[:-1] will pick all the elements from the list but the last one

To check what comes after the last .:

s.split('.')[-1]

Another way is to use rsplit. It works the same way as split but if you provide maxsplit parameter it'll split the string starting from the end:

rest, last = s.rsplit('.', 1)

'classes.students'
'grades'

You can also use re.sub to substitute the part after the last . with an empty string:

re.sub('\.[^.]+$', '', s)

And the last part of your question to wrap words in [] i would recommend to use format and list comprehension:

''.join("[{}]".format(e) for e in s.split('.'))

It'll give you the desired output:

[classes][students][grades]
like image 127
midori Avatar answered Oct 07 '22 23:10

midori


The best way to do this is using the rsplit method and pass in the maxsplit argument.

>>> s = "classes.students.grades"
>>> before, after = s.rsplit('.', maxsplit=1) # rsplit('.', 1) in Python 2.x onwards
>>> before
'classes.students'
>>> after
'grades'

You can also use the rfind() method with normal slice operation.

To get everything before last .:

>>> s = "classes.students.grades"
>>> last_index = s.rfind('.')
>>> s[:last_index]
'classes.students'

Then everything after last .

>>> s[last_index + 1:]
'grades'
like image 15
styvane Avatar answered Oct 08 '22 00:10

styvane


if '.' in s, s.rpartition('.') finds last dot in s,
and returns (before_last_dot, dot, after_last_dot):

s = "classes.students.grades"
s.rpartition('.')[0]
like image 13
GingerPlusPlus Avatar answered Oct 07 '22 22:10

GingerPlusPlus


If your goal is to get rid of a final component that's just a single digit, start and end with re.sub():

s = re.sub(r"\.\d$", "", s)

This will do the job, and leave other strings alone. No need to mess with anything else.

If you do want to know about the general case (separate out the last component, no matter what it is), then use rsplit to split your string once:

>>> "hel.lo.there".rsplit(".", 1)
['hel.lo', 'there']

If there's no dot in the string you'll just get one element in your array, the entire string.

like image 4
alexis Avatar answered Oct 07 '22 22:10

alexis