How can i remove the final characters in a string (python) [duplicate] - python

This question already has answers here:
Python truncate a long string
(22 answers)
Closed 5 months ago.
How can I remove the final characters from a string in python until I reach a certain amount of characters?
How can I turn: abcdefghijklmnopqrstuvwxyz => abcdefghijklmnopqr; using python?

Try string slicing.
You can use negative indexing:
'abcdefghijklmnopqrstuvwxyz'[:-8]
or positive:
'abcdefghijklmnopqrstuvwxyz'[:18]
Pick your poison

You can treat the string as an array of symbols
c_1 = "abcdefghijklmnopqrstuvwxyz"
l = 18
c_2 = c_2[:l]

Related

How to get the numbers from a string (contains no spaces between letters and numbers)? [duplicate]

This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
Closed 3 years ago.
So, I have a string "AB256+74POL". I want to extract the numbers only into a list say num = [256,74]. How to do this in python?
I have tried string.split('+') and followed by iterating over the two parts and adding the characters which satisfy isdigit(). But is there an easier way to that?
import re
a = 'AB256+74POL'
array = re.findall(r'[0-9]+', a)
"".join([c if c.isdigit() else " " for c in mystring]).split()
Explanation
Strings are iterable in python. So we iterate on each character in the string, and replace non digits with spaces, then split the result to get all sequences of digits in a list.

List element case conversion [duplicate]

This question already has answers here:
Convert a list with strings all to lowercase or uppercase
(13 answers)
Closed 4 years ago.
I have a list that has 12 elements. I am getting an input and matching that input with the value of another variable. Now that means that case-sensitivity will be a problem. I know how to go through the list with a loop but how can I convert every character in each element to a lowercase character?
for i in sa:
# something here to convert element in sa to lowercase
A simple one liner:
lowercase_list = [ i.lower() for i in input_list ]

Split the string into words [duplicate]

This question already has answers here:
Split string every nth character?
(19 answers)
Closed 4 years ago.
how to split a string into words of 2 letters. Like given string is "HelloThere" now i want to make it ["He","ll","oT","he","re"]. Please help to code that in python.
yourList = []
yourString = "HelloThere"
while yourString:
yourList.append(yourString[:2])
yourString = yourString[2:]
If you print yourList, you will get the result.

Capture repeated characters and split using Python [duplicate]

This question already has answers here:
How can I tell if a string repeats itself in Python?
(13 answers)
Closed 3 years ago.
I need to split a string by using repeated characters.
For example:
My string is "howhowhow"
I need output as 'how,how,how'.
I cant use 'how' directly in my reg exp. because my input varies. I should check the string whether it is repeating the character and need to split that characters.
import re
string = "howhowhow"
print(','.join(re.findall(re.search(r"(.+?)\1", string).group(1), string)))
OUTPUT
howhowhow -> how,how,how
howhowhowhow -> how,how,how,how
testhowhowhow -> how,how,how # not clearly defined by OP
The pattern is non-greedy so that howhowhowhow doesn't map to howhow,howhow which is also legitimate. Remove the ? if you prefer the longest match.
lengthofRepeatedChar = 3
str1 = 'howhowhow'
HowmanyTimesRepeated = int(len(str1)/lengthofRepeatedChar)
((str1[:lengthofRepeatedChar]+',')*HowmanyTimesRepeated)[:-1]
'how,how,how'
Works When u know the length of repeated characters

NLTK RegexpTokenizer: Regex to retain just characters in Random text [duplicate]

This question already has answers here:
Using explicitly numbered repetition instead of question mark, star and plus
(4 answers)
Closed 5 years ago.
I used tokenizer = RegexpTokenizer(r'\w+') which retains alphanumeric characters
But how do I combine a regular expression to remove every other element retaining just characters greater than length 2
Below is one row in the dataframe which contains random text
0 [ANOTHER 2'' F/P SAMPLE 01:52 ...A13232 / AS OUTPUT MSG...
I think you need for find words with len>2:
RegexpTokenizer(r'\w{3,}')
Or if need only letters:
RegexpTokenizer(r'[a-zA-Z]{3,}')

Categories

Resources