-1

I've been at this for a while now, and I've gotten so close.

I've had two problems to complete for an assignment. The first one I finished. It's supposed to return the first three indices of the given string, and if the string is less than 3 letters, it returns nothing. It looks like this:

enter image description here

The second one is similar, but a little more involved. The problem is:

enter image description here

Hopefully that explains my issue. I can't figure out how to get the inputted number to correspond to the number of indices I'm trying to print. In the first problem, it was simple, because it was always just the first three indices that were used. Now, it's (n) number of indices.

Any help is much appreciated (I'm using Python 3.4.2)

Brian
  • 47
  • 3
  • 9
  • How do we suppose to copy paste image into code? – Maroun Jan 19 '15 at 08:40
  • I'm not sure what you're asking exactly. – Brian Jan 19 '15 at 08:43
  • sounds like homework to me, you could check the string-members. if you do not find anything, write a for loop to do as you please – Zaiborg Jan 19 '15 at 08:43
  • @Zaiborg It is homework, but I'm very new to Python and unfortunately my textbook hasn't come in the mail yet, so I'm basically left for dead with these homework problems. – Brian Jan 19 '15 at 08:46
  • @Brian: try to read [the Python tutorial (it is available online and as e-book in various formats)](https://docs.python.org/dev/tutorial/). If it is too complex for you then try [Learn Python The Hard Way](http://learnpythonthehardway.org/) (it is available online for free). See also [Python For Beginners](https://www.python.org/about/gettingstarted/) and [Python for Non-Programmers](https://wiki.python.org/moin/BeginnersGuide/NonProgrammers). There are [tons of resources for beginners on the internet](http://stackoverflow.com/q/3088/4279). – jfs Jan 20 '15 at 14:46

4 Answers4

3

Strings support sub-stringing in Python.

def returnN(string, length):
    return string[:length] if len(string) >= length else ''

In action:

>>> returnN('hello', 2)
'he'
>>> returnN('hello', 5)
'hello'
>>> returnN('BYE', 1)
'B'
>>> returnN('BYE', 10)
''
Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
  • Thanks! However, how would I get a return of nothing, or ' ', if the number exceeds the number of characters in the string, like in that last example in action you used? – Brian Jan 19 '15 at 08:44
  • There you go. I didn't notice that at first, I thought that was what you were having trouble with. – Inbar Rose Jan 19 '15 at 08:51
  • I went with @zoosuck's answer, but this was helpful! If nothing else, I'll use it to try and understand the process a bit more. Thanks :) – Brian Jan 19 '15 at 08:55
1

Use len and slice method of string method like:

def returnN(string, length):
  length_string = len(string)
  if length > length_string:
    return ''
  return string[0:length]

print(returnN('hello', 5))
print(returnN('hello', 2))
print(returnN('Nye', 1))
print(returnN('OKOK', 10))

or simple way:

def returnN_S(string, length):
  return string[: length] if length <= len(string) else ''

print(returnN_S('hello', 5))
print(returnN_S('hello', 2))
print(returnN_S('Nye', 1))
print(returnN_S('OKOK', 10))

or one line way:

returnN_H = lambda string, length: string[: length] if length <= len(string) else ''
print(returnN_H('hello', 5))
print(returnN_H('hello', 2))
print(returnN_H('Nye', 1))
print(returnN_H('OKOK', 10))

Hope helps.

lqhcpsgbl
  • 3,694
  • 3
  • 21
  • 30
  • This does help a lot! I went with your first example. Thanks :) – Brian Jan 19 '15 at 08:54
  • And you can try to understand the other two for learn more things in Python. – lqhcpsgbl Jan 19 '15 at 08:55
  • 1
    You don't need `length_string = len(string)` there is no cost to calling `len()` on something. There is more cost in creating and keeping a second variable just to keep that information. – Inbar Rose Jan 19 '15 at 09:02
  • @Inbar Rose, I know that, but it simple to maintenance the code if the function become large, in this case, it's not necessary indeed. – lqhcpsgbl Jan 19 '15 at 09:07
0

Unicode strings in Python are immutable Unicode codepoint sequences.
Unicode codepoint is just a number from 0 to sys.maxunicode e.g., Nº128516: ('\U0001f604').
A substring/slice of a Unicode string s such as s[:2] returns a new Unicode string that contains 2 Unicode codepoints.
len(s) returns the number of Unicode codepoints in the string s.
An empty string could be represented using '' string literal in Python source code.


To return a given number of user-perceived characters from a string or an empty string if the text size is too small, you could use \X regular expression (it matches an eXtended grapheme cluster):

#!/usr/bin/env python3
import regex # $ pip install regex

def returnN(text, n):
    chars = regex.findall(r'\X', text)
    return ''.join(chars[:n]) if len(chars) >= n else ''

text = 'a\u0300 biento\u0302t'
print(text) # -> à bientôt
print(returnN(text, 4)) # -> à bi

A single user-perceived character such as can span several Unicode codepoints such as U+0061, U+0300.


The term character is ambiguous. It can mean a byte, Unicode codepoint, grapheme cluster in different circumstances.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
-2
input_string = "hello"

def returnN(string, n):
    return string[:n]

output = returnN(input_string, 3)

that's all

glglgl
  • 89,107
  • 13
  • 149
  • 217
Eren V.
  • 210
  • 1
  • 4
  • 12
  • This is incorrect if the string is shorter than n characters. – Holloway Jan 19 '15 at 09:24
  • yes you are right. we can add an if condition before return statement. if len(string) < n: print("n must be smaller than length of string") – Eren V. Jan 19 '15 at 10:19