Let´s say I have a one-word string ("Hello") and I wanted to swap the first and last letter so I´d do this:
s="Hello"
l=list(s)
l[0],l[len(l)-1]=l[len(l)-1],l[0]
print("".join(l))
But, what if I had to swap the first and last letter in every word of the string:"Hello World" , so that I would get "oellH dorlW".
I was thinking using nested lists but it seems overcomplicated.
Strings are immutable, so you can create a new one by slicing:
s = "Hello"
>>> s[-1] + s[1:-1] + s[0]
"oellH"
To do multiple words, split and rejoin as follows:
s= "Hello World"
>>> ' '.join(word[-1] + word[1:-1] + word[0] for word in s.split())
'oellH dorlW'
You can split your string, swap letters for each word and .join() it back together:
# example is wrong, does not swap, only puts first in the back. see below for fix
text = ' '.join( t[1:]+t[0] for t in "Hello World".split() )
print (text)
Output:
elloH orldW
This uses list comprehensionst to extract each splitted word (t) - list slicing to move the front letter to its back (t[1:]+t[0]) and ' '.join() to make the list of strings back to a string.
Links:
What exactly does the .join() method do? and str.join()
Understanding Python's slice notation (for lists, strings are similar)
str.split()
It also works for longer strings:
elloH orldW si a eallyr verusedo trings ermt - orF ureS !
As pointed out by #Accumulation I misread the question - my example simply puts the first letter to the end of the string thats only halve the work done for swapping first and last letter:
# t[-1] is the last character put to the front,
# followed by t[1:-1] 1st to (but not including) the last character
# followed by t[0] the first character
text = ' '.join( t[-1]+t[1:-1]+t[0] for t in "Hello World".split() )
print (text)
Output:
oellH dorlW
string = "Hello Planet Earth"
Make a list of words by splitting on space char
words = string.split(" ")
Then iterate on that list with your script
for word in words:
l = list(word)
l[0], l[len(l) - 1] = l[len(l) - 1], l[0]
print("".join(l))
Related
I have a python challenge that if given a string with '_' or '-' in between each word such as the_big_red_apple or the-big-red-apple to convert it to camel case. Also if the first word is uppercase keep it as uppercase. This is my code. Im not allowed to use the re library in the challenge however but I didn't know how else to do it.
from re import sub
def to_camel_case(text):
if text[0].isupper():
text = sub(r"(_|-)+"," ", text).title().replace(" ", "")
else:
text = sub(r"(_|-)+"," ", text).title().replace(" ", "")
text = text[0].lower() + text[1:]
return print(text)
Word delimiters can be - dash or _ underscore.
Let's simplify, making them all underscores:
text = text.replace('-', '_')
Now we can break out words:
words = text.split('_')
With that in hand it's simple to put them back together:
text = ''.join(map(str.capitalize, words))
or more verbosely, with a generator expression,
assign ''.join(word.capitalize() for word in words).
I leave "finesse the 1st character"
as an exercise to the reader.
If you RTFM you'll find it contains a wealth of knowledge.
https://docs.python.org/3/library/re.html#raw-string-notation
'+'
Causes the resulting RE to match 1 or more repetitions of the preceding RE. ab+ will match ‘a’ followed by any non-zero number of ‘b’s
The effect of + is turn both
db_rows_read and
db__rows_read
into DbRowsRead.
Also,
Raw string notation (r"text") keeps regular expressions sane.
The regex in your question doesn't exactly
need a raw string, as it has no crazy
punctuation like \ backwhacks.
But it's a very good habit to always put
a regex in an r-string, Just In Case.
You never know when code maintenance
will tack on additional elements,
and who wants a subtle regex bug on their hands?
You can try it like this :
def to_camel_case(text):
s = text.replace("-", " ").replace("_", " ")
s = s.split()
if len(text) == 0:
return text
return s[0] + ''.join(i.capitalize() for i in s[1:])
print(to_camel_case('momo_es-es'))
the output of print(to_camel_case('momo_es-es')) is momoEsEs
r"..." refers to Raw String in Python which simply means treating backlash \ as literal instead of escape character.
And (_|-)[+] is a Regular Expression that match the string containing one or more - or _ characters.
(_|-) means matching the string that contains - or _.
+ means matching the above character (- or _) than occur one or more times in the string.
In case you cannot use re library for this solution:
def to_camel_case(text):
# Since delimiters can have 2 possible answers, let's simplify it to one.
# In this case, I replace all `_` characters with `-`, to make sure we have only one delimiter.
text = text.replace("_", "-") # the_big-red_apple => the-big-red-apple
# Next, we should split our text into words in order for us to iterate through and modify it later.
words = text.split("-") # the-big-red-apple => ["the", "big", "red", "apple"]
# Now, for each word (except the first word) we have to turn its first character to uppercase.
for i in range(1, len(words)):
# `i`start from 1, which means the first word IS NOT INCLUDED in this loop.
word = words[i]
# word[1:] means the rest of the characters except the first one
# (e.g. w = "apple" => w[1:] = "pple")
words[i] = word[0].upper() + word[1:].lower()
# you can also use Python built-in method for this:
# words[i] = word.capitalize()
# After this loop, ["the", "big", "red", "apple"] => ["the", "Big", "Red", "Apple"]
# Finally, we put the words back together and return it
# ["the", "Big", "Red", "Apple"] => theBigRedApple
return "".join(words)
print(to_camel_case("the_big-red_apple"))
Try this:
First, replace all the delimiters into a single one, i.e. str.replace('_', '-')
Split the string on the str.split('-') standardized delimiter
Capitalize each string in list, i.e. str.capitilize()
Join the capitalize string with str.join
>>> s = "the_big_red_apple"
>>> s.replace('_', '-').split('-')
['the', 'big', 'red', 'apple']
>>> ''.join(map(str.capitalize, s.replace('_', '-').split('-')))
'TheBigRedApple'
>> ''.join(word.capitalize() for word in s.replace('_', '-').split('-'))
'TheBigRedApple'
If you need to lowercase the first char, then:
>>> camel_mile = lambda x: x[0].lower() + x[1:]
>>> s = 'TheBigRedApple'
>>> camel_mile(s)
'theBigRedApple'
Alternative,
First replace all delimiters to space str.replace('_', ' ')
Titlecase the string str.title()
Remove space from string, i.e. str.replace(' ', '')
>>> s = "the_big_red_apple"
>>> s.replace('_', ' ').title().replace(' ', '')
'TheBigRedApple'
Another alternative,
Iterate through the characters and then keep a pointer/note on previous character, i.e. for prev, curr in zip(s, s[1:])
check if the previous character is one of your delimiter, if so, uppercase the current character, i.e. curr.upper() if prev in ['-', '_'] else curr
skip whitepace characters, i.e. if curr != " "
Then add the first character in lowercase, [s[0].lower()]
>>> chars = [s[0].lower()] + [curr.upper() if prev in ['-', '_'] else curr for prev, curr in zip(s, s[1:]) if curr != " "]
>>> "".join(chars)
'theBigRedApple'
Yet another alternative,
Replace/Normalize all delimiters into a single one, s.replace('-', '_')
Convert it into a list of chars, list(s.replace('-', '_'))
While there is still '_' in the list of chars, keep
find the position of the next '_'
replacing the character after '_' with its uppercase
replacing the '_' with ''
>>> s = 'the_big_red_apple'
>>> s_list = list(s.replace('-', '_'))
>>> while '_' in s_list:
... where_underscore = s_list.index('_')
... s_list[where_underscore+1] = s_list[where_underscore+1].upper()
... s_list[where_underscore] = ""
...
>>> "".join(s_list)
'theBigRedApple'
or
>>> s = 'the_big_red_apple'
>>> s_list = list(s.replace('-', '_'))
>>> while '_' in s_list:
... where_underscore = s_list.index('_')
... s_list[where_underscore:where_underscore+2] = ["", s_list[where_underscore+1].upper()]
...
>>> "".join(s_list)
'theBigRedApple'
Note: Why do we need to convert the string to list of chars? Cos strings are immutable, 'str' object does not support item assignment
BTW, the regex solution can make use of some group catching, e.g.
>>> import re
>>> s = "the_big_red_apple"
>>> upper_regex_group = lambda x: x.group(1).upper()
>>> re.sub("[_|-](\w)", upper_regex_group, s)
'theBigRedApple'
>>> re.sub("[_|-](\w)", lambda x: x.group(1).upper(), s)
'theBigRedApple'
I need to take the initial letter of every word, moving it to the end of the word and adding 'arg'. For such I tried the following way
def pirate(str):
list_str = str.split(' ')
print(list_str)
new_str = ''
for lstr in list_str:
first_element = lstr[0]
second_element = lstr[1:]
new_str += second_element + first_element + 'arg' + ' '
return new_str
print(pirate('Hello! how are, you!!'))
The expected output is: elloHarg! owharg reaarg, ouyarg!!
However, I am getting following output: ello!Harg owharg re,aarg ou!!yarg
How can I make it work the following usecase?
Punctuations should remain at the end of the word even after translation. Assume Punctuations wont appear after than end of the word. Punctuations to be considered are .,:;?! There could be multiple punctuations present (e.g yes!!)
Here is a short and efficient solution using a regex:
import re
re.sub(r'(\w)(\w+)', r'\2\1arg', 'Hello! how are, you!!')
This is literally: replace each single letter followed by more letters by the more letters first, then the single letter and 'arg'
Output:
'elloHarg! owharg reaarg, ouyarg!!'
As a function:
def pirate(s):
return re.sub(r'(\w)(\w+)', r'\2\1arg', s)
My requirements
Use Python to create a function cleanstring(S) to "clean up" the spaces in a sentence S.
The sentence may have extra spaces at the front and/or at the end and/or between words.
The subroutine returns a new version of the sentence without the extra spaces.
That is, in the new string, the words should be the same but there should be no spaces at the start, only one space between each word and no spaces at the end.
This program is about you writing code to search through a string to find words and so you are not allowed to use the split function in Python.
You can solve this problem with the basic capabilities of the if and while statements and string operations of len and concatentation.
For example: if the input is: " Hello to the world !" then the output should be: "Hello to the world!"
Question
My program creates an error.
How do I fix the error in my program?
def cleanupstring (S):
newstring = ["", 0]
j = 1
for i in range(len(S)):
if S[i] != " " and S[i+1] != " ":
newstring[0] = newstring[0] + S[i]
else:
newstring[1] = newstring [1] + 1
return newstring
# main program
sentence = input("Enter a string: ")
outputList = cleanupstring(sentence)
print("A total of", outputList[1], "characters have been removed from your
string.")
print("The new string is:", outputList[0])
Different methods can be used for removing leading and trailing spaces, for converting multiple spaces to one and to remove spaces before exclamation mark, comma etc:
mystr = " Hello . To , the world ! "
print(mystr)
mystr = mystr.strip() # remove leading and trailing spaces
import re # regex module
mystr = re.sub(r'\s+'," ", mystr) # convert multiple spaces to one space.
mystr = re.sub(r'\s*([,.!])',"\\1", mystr) # remove spaces before comma, period and exclamation etc.
print(mystr)
Output:
Hello . To , the world !
Hello. To, the world!
The solutions in comment are correct. You are getting an erro because you try to access S[i+1] in loop for i in range(len(S)):
Solution
Loop only till second-last element
for i in range(len(S) - 1):
Suggestion
As you said you can't use spit() function, so assuming that you can use other functions (to modify string, not to extract words), strip() function and a little bit of regular expressions will do what your cleanupstring() is trying to do.
Code
def cleanupstring (S):
newstring = ["", 0]
init_length = len(S)
S = S.strip() #remove space from front and end
S = re.sub(r'\s+'," ", S) #remove extra space from between words
newstring[0] = S
newstring[1] = init_length - len(S)
return newstring
# main program
sentence = input("Enter a string: ")
outputList = cleanupstring(sentence)
print("A total of", outputList[1], "characters have been removed from your
string.")
print("The new string is:", outputList[0])
If I have a string like this: ABCDE
I want to read two characters at a time (AB then CD) and remove the remaining characters (E) which cannot be read in tuples or in two's. How would I remove those characters?
I have this code below so far:
s = 'ABCDE'
for (first, second) in zip(s[0::2], s[1::2]):
if not first or not second:
if first:
s.replace(first, '')
continue
else:
s.replace(second, '')
continue
print first, second
print s
This code prints (A B C D) which is good but I want to remove that extra E in the for loop which I am trying to do with the if statement. I check if the either the first or second variable of the tuple is an empty string and then remove whichever one isn't an empty string from the original s variable.
This above code doesn't seem to work. Does anyone have a different suggestion or how I can improve this?
If you want to remove the last character in case the string's length is odd:
word = "ABCDE"
if len(word) % 2 == 1:
word = word[:-1]
Now if you want to read the characters two at a time, here is a more instinctive way:
for i in range(len(word) // 2):
print(word[2*i:2*i+2])
The latter will even drop the last character for you.
str = "ABCDE"
for i, k in zip(str[::2], str[1::2]):
print(i + k)
Outputs:
AB
CD
I have a two dimensional array called "beats" with a bunch of data. In the second column of the array, there is a list of words in alphabetical order.
I also have a sentence called "words" which was originally a string, which I've turned into an array.
I need to check if one of the words in "words" matches any of the words in the second column of the array "beats". If a match has been found, the program changes the matched word in the sentence "words" to "match" and then return the words in a string. This is the code I'm using:
i = 0
while i < len(words):
n = 0
while n < len(beats):
if words[i] == beats[n][1]:
words[i] = "match"
n = n + 1
i = i + 1
mystring = ' '.join(words)
return mystring
So if I have the sentence:
"Money is the last money."
And "money" is in the second column of the array "beats", the result would be:
"match is the last match."
But since there's a period behind "match", it doesn't consider it a match.
Is there a way to ignore punctuation when comparing the two strings? I don't want to strip the sentence of punctuation because I want the punctuation to be in tact when I return the string once my program's done replacing the matches.
You can create a new string that has the properties you want, and then compare with the new string(s). This will strip everything but numbers, letters, and spaces while making all letters lowercase.
''.join([letter.lower() for letter in ' '.join(words) if letter.isalnum() or letter == ' '])
To strip everything but letters from a string you can do something like:
from string import ascii_letters
''.join([letter for letter in word if letter in ascii_letters])
You could use a regex:
import re
st="Money is the last money."
words=st.split()
beats=['money','nonsense']
for i,word in enumerate(words):
if word=='match': continue
for tgt in beats:
word=re.sub(r'\b{}\b'.format(tgt),'match',word,flags=re.I)
words[i]=word
print print ' '.join(words)
prints
match is the last match.
If it is only the fullstop that you are worried about, then you can add another if case to match that too. Or similar you can add custom handling if your cases are limited. or otherwise regex is the way to go.
words="Money is the last money. This money is another money."
words = words.split()
i = 0
while i < len(words):
if (words[i].lower() == "money".lower()):
words[i] = "match"
if (words[i].lower() == "money".lower() + '.'):
words[i] = "match."
i = i + 1
mystring = ' '.join(words)
print mystring
Output:
match is the last match. This match is another match.