Python Forum
calculating length of string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: calculating length of string (/thread-2212.html)

Pages: 1 2


calculating length of string - desul - Feb-27-2017

Dear All,

how can I calculate the length of the string, for e.g

   len(ontology)= 8

but I need it should return 1..... PLEASE TELL ME ANY COMMAND FOR IT IN PYTHON.....


THANK YOU IN ADVANCES


RE: calculating length of string - micseydel - Feb-27-2017

Are you saying you want the length of the string "ontology" to be one? Could you elaborate a bit on your question?


RE: calculating length of string - desul - Feb-27-2017

actually, I want to calculate a length of term

for e.g [information exchange]

len([information exchanhe])= 19

but I want it to return 2 instead 19 bcz two terms

len([ontology ])= 9

but i want it to return 1 bcz single term


RE: calculating length of string - micseydel - Feb-27-2017

A simple solution to this would be to split on spaces and get the length of that.

What's the context for this? A homework assignment? Or some other project?

In either case, if you write code giving it a try we're more likely to help along toward fully completing the code.


RE: calculating length of string - desul - Feb-27-2017

domain = "statistical"

split_domain = domain.split(" ")

for part in split_domain:
print(len(part))
returns 11 but i want 1
11


RE: calculating length of string - Larz60+ - Feb-27-2017

Do it this way:
domain = "statistical"

split_domain = domain.split(" ")

print(len(split_domain))



RE: calculating length of string - buran - Feb-27-2017

Please, use python code tags, when posting code. This helps preserve the indentation.
What you describe as desired result is number of words in the string, not length of the string itself. words being defined as one or more constitutive non-blank chars. what micseydel  suggest is the most simple approach.

>>> mystr = 'statistical'
>>> len(mystr.split(' '))
1
>>> mystr = 'some string'
>>> len(mystr.split(' '))
2
>>> mystr = 'yet another string'
>>> len(mystr.split(' '))
3
however you need to take into account multiple spaces, new line, etc.


>>> mystr = 'some   string'
>>> len(mystr.split(' '))
4
>>> print mystr.split(' ')
['some', '', '', 'string']



RE: calculating length of string - desul - Feb-27-2017

thank you ..............buran


RE: calculating length of string - Ofnuts - Feb-27-2017

(Feb-27-2017, 07:15 AM)buran Wrote: however you need to take into account multiple spaces, new line, etc.

split() without arguments will do just that:
Output:
>>> x='abcd   efgh\tijkl\n  mnopq' >>> print x abcd   efgh     ijkl   mnopq >>> x.split() ['abcd', 'efgh', 'ijkl', 'mnopq']



RE: calculating length of string - nilamo - Mar-22-2017

Regular expressions are cool sometimes, too:
>>> import re
>>> regex = re.compile(r"\s+")
>>> regex.split("hello there\tevery     body!")
['hello', 'there', 'every', 'body!']