Splitting Python string - python

Split the string into pairs of two characters. If the string contains an odd number of characters, then the missing second character of the final pair should be replaced with an underscore ('_').
Input: A string.
Output: An iterable of strings.
Example:
split_pairs('abcd') == ['ab', 'cd']
split_pairs('abc') == ['ab', 'c_']

import textwrap
def split_pairs(input):
# Use textwrap to split the input into chunks of two characters
split = textwrap.wrap(input, 2)
# In your example I see you want a "_" if string is odd length
# Check the length of the last chunk, and if it is 1, add a "_"
if len(split[-1]) == 1:
split[-1] += "_"
return split
print(split_pairs('abcd'))
print(split_pairs('abc'))

st = input('Input a string:')
arr = []
if len(st)%2==0:
for i in range(0,len(st)-1,2):
arr.append(st[i]+st[i+1])
else:
st +='_'
for i in range(0,len(st)-1,2):
arr.append(st[i]+st[i+1])
print(arr)
also if you want to enter a long text and strip white spaces after it try st = st.replace(' ','') after the input:
st = input('Input a string:')
st = st.replace(' ','')
arr = []
if len(st)%2==0:
for i in range(0,len(st)-1,2):
arr.append(st[i]+st[i+1])
else:
st +='_'
for i in range(0,len(st)-1,2):
arr.append(st[i]+st[i+1])
print(arr)

Try this short function without imports:
def split_pairs(inp):
pairs = [inp[2*i:2*i+2] for i in range(len(inp) // 2)]
if len(inp) % 2 == 1:
pairs.append(f'{inp[-1]}_')
return pairs

My solution is:
import re
def solution(s):
return re.findall(".{2}", s + "_")

Related

How to acces the next value in for loop

So im trying to get first letters of words(excluding first word, i already solved that) in a sentence.
But it appends spaces to the list.
Would appreciate if you help.
Here's the code:
lst = []
for t in (input()):
if t == " ":
lst.append(t)
print(*lst, sep="")
input1: asd dfd yjs
output1: dy
just this:
''.join([s[0] for s in input().split()[1:]])
step by step:
if input() returns asd dfd yjs
split string (more):
input().split() # output: ['asd', 'dfd', 'yjs']
sub list (more):
input().split()[1:] # output: ['dfd', 'yjs']
one line loop (more):
[s[0] for s in ['dfd', 'yjs']] # output: ['d', 'y']
sub string (more):
s="dfd"
s[0] # output: d
concat list of strings (more):
''.join(['d', 'y']) # output: dy
You're getting spaces because that's what you asked for. Read your code out loud and it will probably make sense:
if t == " ":
lst.append(t)
If t is a space, append it to lst
Seems clear that you will only get spaces.
You want the character after t to be appended. There's two ways to do that using your for loop method: 1) if t is a space, append the next character; 2) if the previous character was a space, append t. Here's how you might implement #2:
lst = []
prev_char = None
for t in (input()):
if prev_char == " ":
lst.append(t)
prev_char = t
print(*lst, sep="")
This will print the first character of ever word except the first word. Initialize last_char to a space to include the first word.
You may
split your sentence into words using x.split()
remove the first word, using a slice [1:] (from index 1 to the end)
then keep only the first char of each word and concatenate it to a result string
x = input(">")
result = ""
for word in x.split()[1:]:
result += word[0]
print(result) # dy
Using a generator and str.join :
x = input(">")
result = ''.join(word[0] for word in x.split()[1:])
You could use str.split:
lst = [s[0] for s in input().split()[1:]]
A simple example:
lst = []
get_next = False
for t in input():
if t == " ":
get_next = True
elif get_next:
lst.append(t)
get_next = False
print(*lst, sep="")
A lot great answers already and this is good case for split. If you specifically need to collect the next token after a special token in a stream of tokens, here are some other options:
inp = "asd dfd yjs"
lst = []
for a, b in zip(inp[:-1],inp[1:]):
if a == " ":
lst.append(b)
print(*lst, sep="")
# With comprehensions - my choice
print("".join([b for a, b in zip(inp[:-1],inp[1:]) if a == " "]))
# With functional approach
from functools import reduce
from operator import add, itemgetter
def has_prior_space(x):
return x[0] == " "
print(reduce(add, map(itemgetter(1), filter(has_prior_space, zip(inp[:-1], inp[1:])))))
In Python 3.10, there will be a new pairwise iterator that does this type of "2 at a time" iteration specifically: zip(inp[:-1],inp[1:])
Use Array join():
''.join(lst)

Appending a char to an empty list

I am very new to programming, so sorry for a basic question. I am trying to write a function that will take a string in which words are divided by ',' and return a list of these words (the Split method). My code is:
def str_to_list(my_shop_str):
my_shop_list = ['']
word_in_list = 0
for letter in my_shop_str:
if letter != ',':
my_shop_list[word_in_list] += letter
else:
word_in_list += 1
my_shop_list + ['']
return my_shop_list
print(str_to_list("Milk,Cottage,Tomatoes")) should look like [Milk, Cottage, Tomatoes]
but I am keep getting IndexError: list index out of range.
I read some answers here and couldn't find something to work.
Can anyone explain what is wrong.
list has the method append so a solution will be something like this:
def str_to_list(my_shop_str):
my_shop_list = ['']
word_in_list = 0
for letter in my_shop_str:
if letter != ',':
my_shop_list[word_in_list] += letter
else:
word_in_list += 1
my_shop_list.append('')
return my_shop_list
PS: Do not forgot about empty spaces between words in string like "aaa, bbb, ccc" will be ['aaa', ' bbb', ' ccc'] with spaces.
def sp(s):
l =[]
while True:
comma = s.find(',')
if comma == -1:
l.append(s)
break
l.append(s[:comma])
s = s[comma+1:]
print(l)
this is a simplified version hope it helps.
Simplest Way:
We can use an inbuilt split function.
def Convert(string):
# split the string whenever "," occurs
# store the splitted parts in a list
li = list(string.split(","))
return li
# Driver code
str1 = "Hello,World"
print(Convert(str1))
Output:
["Hello", "World"]

How to delete all the consecutive characters in a string and print the remaining string?

for a string 'mississipie' remove all the consecutive repeating characters and print the remaining.
sample input:
'mississipie'
sample output:
'mpie'
A recursive version with itertools.groupby:
from itertools import groupby
s = 'mississipie'
def remove(s):
out = ''
for _, g in groupby(s):
tmp = ''.join(g)
if len(tmp) == 1:
out += tmp
if out == s:
return out
return remove(out)
print(remove(s))
Prints:
mpie
Using re.sub with a while loop, we can try successively removing clusters of two or more repeating characters from the input. We will iterate doing this, until no more replacements have been made. This is how we know when to stop replacing.
inp = "mississipie"
length = len(inp)
while True:
inp = re.sub(r'(.)\1+', '', inp)
if len(inp) == length:
break
length = len(inp)
print("final output: " + inp)
This prints:
final output: mpie
Here are the steps of replacement:
mississipie
miiipie (remove 'ss', twice)
mpie (remove 'iii' cluster, once)

Print the substring that is present between two uppercase letters in a string

Here is my code:
x = str(input())
w = ' '
w2 = ''
for i in x:
if i.isupper() == True:
w2 += w
else:
w2 += i
print(w2)
I have converted the uppercase alphabets to space, can anyone suggest me what to do next?
Input: abCdefGh
Expected output: def
To print the substring that is present between two uppercase letters in the string.
Step 1: Find the index position of the uppercase letter.
Step 2: Then slice the substring between the first element + 1 and the second element in the list (string[start: end])
word = "abCdefGh"
# Get indices of the capital letters in a string:
def getindices(word):
indices = []
for index, letter in enumerate(word):
if (letter.isupper() == True):
indices.append(index)
return indices
if __name__ == "__main__":
caps_indices = getindices(word)
start = caps_indices[0] + 1
end = caps_indices[1]
print(word[start:end])
Output:
def
I use a flag to tag whether program should add character i into w2.
Notice the condition I write. It is the key of my code.
x = "abCdefGh"
w2 = ''
inBetween = False
for i in x:
if i.isupper():
# Upper case denotes either start or end of substring
if not inBetween:
# start of substring
inBetween = True
continue
else:
# end of substring
inBetween = False
break
if inBetween:
w2+= i
print(w2)
Result is :
def
You can use regex to extract text easily.
import re
# pattern to accept everything between the capital letters.
pattern = '[A-Z](.*?)[A-Z]'
# input string
input_string= 'thisiSmyString'
matched_string = re.findall(pattern, input_string)
print(matched_string[0])

Find out word at specific index

I have a string with multiple words separated by underscores like this:
string = 'this_is_my_string'
And let's for example take string[n] which will return a letter.
Now for this index I want to get the whole word between the underscores.
So for string[12] I'd want to get back the word 'string' and for string[1] I'd get back 'this'
Very simple approach using string slicing is to:
slice the list in two parts based on position
split() each part based on _.
concatenate last item from part 1 and first item from part 2
Sample code:
>>> my_string = 'this_is_my_sample_string'
# ^ index 14
>>> pos = 14
>>> my_string[:pos].split('_')[-1] + my_string[pos:].split('_')[0]
'sample'
This shuld work:
string = 'this_is_my_string'
words = string.split('_')
idx = 0
indexes = {}
for word in words:
for i in range(len(word)):
idx += 1
indexes[idx] = word
print(indexes[1]) # this
print(indexes[12]) #string
The following code works. You can change the index and string variables and adapt to new strings. You can also define a new function with the code to generalize it.
string = 'this_is_my_string'
sp = string.split('_')
index = 12
total_len = 0
for word in sp:
total_len += (len(word) + 1) #The '+1' accounts for the underscore
if index < total_len:
result = word
break
print result
A little bit of regular expression magic does the job:
import re
def wordAtIndex(text, pos):
p = re.compile(r'(_|$)')
beg = 0
for m in p.finditer(text):
#(end, sym) = (m.start(), m.group())
#print (end, sym)
end = m.start()
if pos < end: # 'pos' is within current split piece
break
beg = end+1 # advance to next split piece
if pos == beg-1: # handle case where 'pos' is index of split character
return ""
else:
return text[beg:end]
text = 'this_is_my_string'
for i in range(0, len(text)+1):
print ("Text["+str(i)+"]: ", wordAtIndex(text, i))
It splits the input string at '_' characters or at end-of-string, and then iteratively compares the given position index with the actual split position.

Categories

Resources