2

How to delete a last word in the string in Python?

 2 years ago
source link: https://www.codesd.com/item/how-to-delete-a-last-word-in-the-string-in-python.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to delete a last word in the string in Python?

advertisements

For example if I have a string like this:

a = "[email protected]:/home/hello/there"

How do I remove the last word after the last /. The result should be like this:

[email protected]:/home/hello/ 

OR 

[email protected]:/home/hello


Try this:

In [6]: a = "[email protected]:/home/hello/there"

In [7]: a.rpartition('/')[0]
Out[7]: '[email protected]:/home/hello'

Related Articles

Android, Java receives only the last word in the string

I have build a java app which use voice recognition, I created a string array like this: String[] greetings = {"hello", "hi", "yow"}; Now the problem is is that the app is only detecting the last word of the array "yow&q

How to get only words from the string using Python

I have a file which has special characters, so I used file operations to read. f=open('st.txt','r') string=f.read() The sample string is "Free Quote!\n \n Protecting your family is the best investment you\'ll eve=\nr \n" now I want to remove all

How to delete patterns or words from the end of the chain back?

I have a string like this: <foo><bar><k2><v1>aaa<b>bbb</b>ccc</v1></k2></bar><foo> I would like to strip the first 3 opening and the last 3 closing tags from the string. I do not know the tag nam

How to extract what I want from the string in Python

I have a program in Python 3.3 which has a string which I would like to just get the stuff off the end of. For example "Mary had a little lamb". I would like to just get the text after 'a' in the string. How can I do this? s = "Mary had a l

With iTerm2 on Mac, how to delete a cursor word on the command line?

With iTerm2 1.0.0 on Mac OS X 10.6.8, I'd like to delete from cursor to the next end of word, i.e. deleting one word forward. I tried Alt+d but this types the delta operator symbol ∂ and doesn't delete. How to suppress the typing but let it delete?I

Delete a specific word from the string

I am using oracle10g. I want to remove all occurrences of particular word from sentence, But I don't want to remove any other word which contains other characters between a-z or A-Z. For example, Following is a sentence from which I want to remove so

How can I insert a word if the string has more than one space in Java?

I have the following code, String s = " Hello I'm a multi spaced String" In string s, there are multiple (indeterminate) spaces; but, I need to print it as %temp%Hello I'm a%temp%multi spaced String How can I do this?Use regex \s{2,} and replace

How to delete an empty paragraph from the string?

In my database, some text are stored as <p>texxxxxxxxxxt1</p> <p> </p> <p>textttttttttt2</p> <p> </p> <p> </p> <p>teeeeeeeeeext3</p> <p> </p> <p> </p> <p>

VBA - Extract the last word from the string and create a new string from the extraction

I have this function for selecting customer Function GetFolder() As String Dim fldr As FileDialog Set fldr = Application.FileDialog(msoFileDialogFolderPicker) With fldr .Title = "Select" .AllowMultiSelect = False If .Show = 0 Then MsgBox ("

How to extract words from the string corresponding to those contained in an array of forbidden words (and delete all remaining blanks)?

How would you extract certain words from a string, given an array of forbidden words. Consider the following Woody Allen quote as an example: Love is the answer, but while you are waiting for the answer sex raises some pretty good questions And this

Replace multiple words in the string in SAS

I am trying to search a string for multiple words, then if any of those words are found, remove them. I wrote the below code which seems to work on some words but not all, and when it does work, it only works on the last word in the string. data read

How do I delete the last word in a string?

How can I remove the last word from a string? For example, with this input: "I am from Tamil Nadu." I want this result: "I am from Tamil." var str = " i am from Tamil nadu."; var res = str.split(" "); //split by spa

How to read the last word or the last line in DocumenListener

I implement DocumentListener for JEditorPane ( need help with method insertUpdate(DocumentEvent e) ). How to read last word or last line (lines are separated by '\n' and words are separated by ' ' ) ?To get the last line in your JEditorPane, split th

How to read the last word or the last word in JTextArea

I am making a Source code Editor using JtextArea.In that I want to do the following task." While user typing on Editor window(JtextArea),each and every time the updated text(last word) should compare with the set of words in the database, if it match

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK