Python Forum
calculating length of string
Thread Rating:
  • 4 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calculating length of string
#1
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
Reply
#2
Are you saying you want the length of the string "ontology" to be one? Could you elaborate a bit on your question?
Reply
#3
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
Reply
#4
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.
Reply
#5
domain = "statistical"

split_domain = domain.split(" ")

for part in split_domain:
print(len(part))
returns 11 but i want 1
11
Reply
#6
Do it this way:
domain = "statistical"

split_domain = domain.split(" ")

print(len(split_domain))
Reply
#7
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']
Reply
#8
thank you ..............buran
Reply
#9
(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']
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#10
Regular expressions are cool sometimes, too:
>>> import re
>>> regex = re.compile(r"\s+")
>>> regex.split("hello there\tevery     body!")
['hello', 'there', 'every', 'body!']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can i limit length string? perrfect 3 13,730 Feb-10-2020, 04:52 PM
Last Post: Jendker
  python gives wrong string length and wrong character thienson30 2 2,941 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Highlight/Underline a string | ValueError: zero length field name in format searching1 1 2,990 Jul-01-2019, 03:06 AM
Last Post: metulburr
  Python find the minimum length of string to differentiate dictionary items zydjohn 3 3,560 Mar-03-2018, 05:23 PM
Last Post: Gribouillis
  Get string length RedSkeleton007 3 4,550 Dec-04-2017, 03:19 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020