Getting number of characters in a unicode string in python [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
How can I get the number of codepoints in a string which may contain unicode characters 3 byte long. https://unicode-table.com/
For example for "I❤U" I would like to get 3.
Doing len(str) returns the number of bytes, so for the above example I would get 5.

Try to decode it in python2:
"I❤U".decode('utf-8')
Output: u'I\u2764U'
then len("I❤U".decode('utf-8')), it will be 3

In my env, I tried your code. But my result of len("I❤U") is 3.
>>> len("I❤U")
3
>>>

Related

Python program for encryption string. a=z, b=y, c=x etc in lower case [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 days ago.
Improve this question
The whitespace should be replaced with a 'dollar' sign.
Keep the punctuation marks unchanged.
Note: Convert the given input sentence into lowercase before encrypting.
The final output should be lowercase.
input = Hello World
expected_output = svool$dliow

need space between two words in python with two inputs [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to put in 2 inputs then get them as an output, but I cant seem to get a space between them at the end . I have tried a + sign, space between x and y, and a comma and none work.
A formatted string will work nicely here.
We can provide variables to a string that has special format markers, like so:
print('{0} {1}'.format(word1, word2))
Or, more easily, a string concatenation:
print(word1 + ' ' + word2)

Using "if not" with multiple string arguments Python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Here is what I currently have, which is not working:
if "Forwarder" not in shp_name or "T_" not in shp_name or "Grad" not in shp_name:
I've also tried:
if ("Forwarder", "T_", "Grad") not in shp_name:
Samples of the input would be "DS_Forwarder_1" or "DS_Harvester_1". The script proceeds directly to else as it's unable to identify any of the above substrings in the primary string.
Try using the any built in.
if any(s in shp_name for s in ("Forwarder", "T_", "Grad")):
...
This will be true if any of the given strings are present in shp_name. You can use if not any(... if you want False when one of the strings is present.

How to make 0 get counted as an integer? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Each time I put 0 at the beginning of my 7 digit code it is ignored and not times by 3. I have a feeling that I need to change something from str() to int() (and vice-versa) but I may be wrong. I would be grateful for assistance in this matter.
Numeric literals starting with 0 are interpreted as being in base 8.
>>> int("755", base=8)
493
>>> 0755
493
>>> input("> ")
> 0755
493
Try using raw_input() instead of input(). Input() evaluates the user input as python code, where raw_input() evaluates the entry as entered.

Strip tabs only at the end of line? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a huge text file, each line has a tab-delimited string. I need to keep all tabs apart from those at the end of each line. I need to keep the carriage return. Any ideas?
I've tried everything on these answers:
How to trim whitespace (including tabs)?
Trimming a string in Python
Strip spaces/tabs/newlines - python
as well as others I've now closed the tabs on.
Just use a regular expression
>>> import re
>>> s="1\t2\t3\t\t\n"
>>> s2=re.sub('\t+\n','\n',s)
>>> s2
'1\t2\t3\n'

Categories