0

I have the following problem:

Let's say s = 'ab'. len(s) = 2. it has 2 indexes: 0 and 1. What I want to do is to make ab of length 1, or have an index of 0.

All help is welcome

Thanks

2 Answers 2

1

I think your question can be answered two ways:

>>> s = 'ab'
>>> s = s[:1]
>>> s
'a'

or you want string stored in collection

>>> s = 'ab'
>>> s = (s,)
>>> s[0]
'ab'

>>> s = 'ab'
>>> s = [s]
>>> s[0]
'ab'
Sign up to request clarification or add additional context in comments.

Comments

1

If I understood your question then I guess you're looking for slicing:

>>> s = 'ab'
>>> s = s[:1]
>>> s
'a'

Update:

You need a list to do that.

>>> d ='abcefg'
>>> it = iter(d)
>>> d = [x + next(it) for x in it] #creates a list
>>> d
['ab', 'ce', 'fg']
>>> d[0]
'ab'
>>> d[1]
'ce'
>>> d[2]
'fg'

Another way using zip:

>>> d ='abcefg'
>>> it = iter(d)
>>> [x+y for x,y in zip(it,it)]
['ab', 'ce', 'fg']

6 Comments

not quiet, for example d ='abcefg'. What I want is d[0] = 'ab', d[1] = 'ce', d[2] = [fg].
@IsaacAltair add such examples in your question, I've updated my solution.
We are not allowed to use lists in our assignment, but it is great to come to know various solutions. thanks = D
@IsaacAltair You can't do that with a string, in a string an index points to only a single character.
@IsaacAltair what is print_board? This has nothing to do with your original question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.