Method of slicing strings starting at the first blank space - python

I believe my professor taught us a way to begin parsing through a string at the first occurrence of a blank space (or any character for that matter) but I seem to have forgotten it. I'm trying to slice through a sentence starting at the first letter of the second word. Can anyone give me some help?
The code would receive a string similar to that shown below:
'Donald Springman 50 98.0\nKenneth Clarke 52 97.3\nRon Martin 51 95.5'
What I need to do is sort the strings by the given last names. So what I am hoping for is a way to simply bypass the first word in the string, and start parsing at the first space, therefore starting at the second word.

If I understand what you mean is:
string = 'word1 word2 word3'
print(string[string.find(' ') + 1:])
outputs:
'word2 word3'

Related

Need help getting specific character through user input

I been trying to learn python a month and a half thu my uni course for my cs degree(1st year) so try to go a lil easy, i decided to redo from where i had problems
This is the question being asked
'''Given a string (someString) and an int (removeChar), create a new variable (newString)
where the char at index removeChar has been removed. Return newString.
Sounds simple but i cant wrap my head around outputting the newstring without the user inputted character
I know to use string slicing but i dont fully see how to use it with taking out the character and making the new string without it.
if i do
somestring = Alex
newstring = somestring[removechar:]
lets say the remove char is 2, the output is ex
however if i do
somestring = Alex
newstring = somestring[:removechar]
I get the output of Al
after that you would think okay so if removechar: is ex and :removechar is al then e is the letter thats changing the whole string.
But if i do [removechar:removechar] then it should take out the one letter but it doesnt nothing is in the output now. So i cant do it like that
I have to use string slicing for this question bc the time we would be doing this we wouldnt be able to use lists, replace, or translate
What am i missing here that you guys all see?
How this works is it gets all character(s) before the letter you want to remove and then because it is a string it adds that with all the character(s) after the letters you want to remove. The reason we add 1 is because if we do not it will include the letter you want to remove.
def remove(removeCharindx, string):
newString = string[:removeCharindx] + string[removeCharindx+1:]
return newString
print(remove(1, "Alex"))

How to grab a specified number of characters after a part of a specified string?

Let's say I have a string defined like this:
string1 = '23h4b245hjrandomstring345jk3n45jkotherrandomstring'
The goal is to grab the 11 characters (these for example '345jk3n45jk') after a part of the string (this part for example 'randomstring') using a specified search term and the specified number of characters to grab after that search term.
I tried doing something like this:
string2 = substring(string1,'randomstring', 11)
I appreciate any help you guys have to offer!
string2 = string1[string1.find("randomstring")+len("randomstring"):string1.find("randomstring")+len("randomstring")+11]
In one line, using split, and supposing that your randomstring is unique in your string, which seems to be the case as you worded out the question :
string1 = '23h4b245hjrandomstring345jk3n45jkotherrandomstring'
randomstring = 'randomstring'
nb_char_to_take = 11
# split using randomstring as splitter, take part of the string after, i.e the second part of the array, and then the 11 first character
result = string1.split(randomstring)[1][:nb_char_to_take]
You can use a simple regular expression like this
import re
s = "23h4b245hjrandomstring345jk3n45jkotherrandomstring"
result = re.findall("randomstring(.{11})", s)[0]
string1 = '23h4b245hjrandomstring345jk3n45jkotherrandomstring'
string2 = string1[10:22]
print(string2)
randomstring
You could use that. Its called string slicing, you basically count the position of the letters and then the first number before the colon is your starting point the second is your ending point when you enter those position numbers you should get whatever is in-between those position, the last is for a different function I highly suggest you search string slicing on YouTube as my explanation wouldn't really help you, and also search up * Find string method* those should hep you get the idea behind those functions. Sorry couldn't be of much help hope the videos help.

How do you remove first and last char from a string inside of a list depending on specific sets of characters in the list. (Python)

Now, I understand that the title does not exactly correlate to what I am attempting to figure out, but hopefully, you will understand when you see my code.
First of all, there is a list containing a bunch of information, I will include only the first three.
memberList = ['uuid_e04a043abc334bd1a2fbd167bdce1673[MVP+] IgrisGuild Master2020/07/21 '
'02:35:052020/08/09 00:58:55',
'uuid_1f12bce8313040a7978d5c51ceb9d82d[VIP] mistercintPrince2020/08/01 '
'00:31:342020/08/08 23:47:53',
'uuid_405e46954f804487ae9c18689f0c351b[MVP+] zoucePrince2020/08/06 '
'20:11:222020/08/08 22:02:04']
Next, I remove the first 37 and the last 38 characters because they are all not relevant and they are all present at the exact same length in the list.
memberList = [e[37:-38] for e in memberList]
I attempted to do something like this, but I can't quite get it down.
for i in range(len(memberList):
if 'Igris' in memberList:
(remove the first 7 and the last 12 from this specific string inside of the list)
What I want it to end up as is as follows.
print(memberList)
Output:
[Igris, mistercint, zouce]
Thank you for your consideration, this is my first time using this site and I'm very new to coding, so please pardon my incorrect formatting.
import re
members = [re.sub( r"([A-Z])", r" \1", member.split()[1]).split()[0] for member in memberList]
print(members)
...should give you what you want.

Very Beginner Python: Replacing Part of a String

I want to know how you can replace one letter of a string without replacing the same letter. For example, let the variable:
action = play sports.
I could substitute "play" for "playing" by doing print(action.replace("play", "playing")
But what if you have to of the same letters?
For example, what if you want to replace the last half of "honeyhoney" into "honeysweet" (Replacing the last half of the string to sweet?
Sorry for the bad wording, I am new to coding and really unfamiliar with this. Thanks!
def replaceLast(str, old, new):
return str[::-1].replace(old[::-1],new[::-1], 1)[::-1]
print(replaceLast("honeyhoney", "honey", "sweet"))
output
honeysweet
so the idea is to reverse the string and the old and new substrings,
so the last substring becomes the first, do a replace and then reverse the returned string once again, and the number 1 is to replace only once and not both matches
Another solution
def replaceLast(str, old, new):
ind = str.rfind(old)
if ind == -1 : return str
return str[:ind] + new + str[ind + len(old):];
print(replaceLast("honeyhoney", "honey", "sweet"))
output
honeysweet
so here we get the string from the beginning to the index of the last substring then we add the new substring and the rest of the string from where the old substring ends and return them as the new string, String.rfind returns -1 in case of no match found and we need to check aginst that to make sure the output is correct even if there is nothing to replace.

How do I reference a different character in a string while iterating through the string in Python?

I'm trying to write a script that can take doubled letters (aa or tt, for instance) and change them to that letter followed by ː, the length symbol (aa would become aː, and tt would become tː). I want to do this by iterating through the string, and replacing any character in the string that's the same as the last one with a ː. How do I do that?
You could try something like this. I iterated through string and checked each letter against the previous letter. If they match it performs the replacement if not it moves on and stores the new previous letter in previousletter. Also I used the .lower() method to mactch letters even if one is capitalized and one is not.
string = "Tthis is a testt of the ddouble letters"
previousletter = string[0]
for letter in string:
if letter.lower() == previousletter.lower():
string = string.replace("%s%s" % (previousletter, letter) , "%s:" % (letter))
previousletter = letter
print(string)
And here is the output:
t:his is a test: of the d:ouble let:ers
I hope this helps and feel free to ask any questions on the code that I used. Happy programming!

Categories

Resources