piotrektnw
piotrektnw

Reputation: 120

Python basics - square brackets

I am at the beginning on my path to code. I've started learning code on my own. I am looking for help with basic python program. My goal is to create basic program in which:

To simplify it:

Here is piece of my code:

text = "Let's check"
text1 = text
print(text)
digit = input('Choose digit between 0-9')
print(int(digit))
print(text1[digit:6])

My problem is to put variable into the square bracket. Unfortunately it does not work. I know the problem begins in two last lines of the code.

I am not looking for ready solution. I would like to kindly ask you to show me way how to solve it. Thank you very much!

Upvotes: 0

Views: 983

Answers (6)

Arman Soni
Arman Soni

Reputation: 11

see you can take input which is ranging from 0 to 9 and want to print the alphabet which comes at that integer position for this you have to take a list in which from first alphabet to 10th alphabets should be there and then if you want to print the user defined alphabet then you have to just give the output by like this..

a=int(input("Enter the place value of the alphabet ranging from 0 to 9: "))
b=['a','b','c','d','e','f','g','h','i','j']
print(a[b])#this will print the output here b is the list and a is the user input in integer form

Upvotes: 0

Lion14706
Lion14706

Reputation: 87

text = "Let's check"  # here index numbers are (L=0, e=1, t=2, '=3, s=4, blank space=5, c=6, h=7... so on)
text1 = text
print(text)
digit = int(input('Choose digit between 0-9:  '))
letter = text[digit]  # you can also put text as text1 coz text = text1
print(letter)

Upvotes: 0

EL MEKRIF Mohammed
EL MEKRIF Mohammed

Reputation: 63

I will try to put it as simple as I can:

text = "Let's check"
digit = int(input("Choose a digit between 0 and 9: "))
letter = text[digit]  # The index [digit] is basically the digit entered at the line above
print(letter)

Upvotes: 1

Dobry Nikolov
Dobry Nikolov

Reputation: 5

I'm quite new into Python and to programming in general, but this is how I'd solve the problem:

letter = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
digit = int(input())
print(letter[digit])

Upvotes: 1

piotrektnw
piotrektnw

Reputation: 120

Thank you @Klaus D. Got it this way:

print('Hello! Below you can find piece of text:')
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eu lacinia velit. Quisque a ante eu metus ullamcorper sagittis. Phasellus nec varius nibh. Cras maximus mauris vel vehicula congue. Morbi nibh tellus, convallis a sollicitudin in, tincidunt vel elit. Cras tincidunt massa metus, scelerisque luctus sapien laoreet in. Suspendisse rutrum dolor vitae neque semper, sed pulvinar felis feugiat. Morbi et aliquam lorem. Quisque nec arcu varius, iaculis nulla eget, vestibulum diam. Nam volutpat felis et sapien porta lobortis."
text1 = text
print(text1)
print('Want to check what is hidden in the text above? Please follow the instructions!')
digit_beg = input('Choose digit between 0-500')
digit_end = input('Choose digit between 0-500 (no smaller than first one)')
digit1 = (int(digit_beg))
digit2 = (int(digit_end))
print('!!!The digits you have chosen are the numbers of following letters in the text. Below you can find is included between letters you have chosen by indicating specific digits.')
print(text1[digit1:digit2])
print('Thank you!')

Upvotes: 0

picklepirate28
picklepirate28

Reputation: 175

This should work:

int_digit = int(digit)
print(int_digit) # not necessary, leave this if you want to print the user input as an integer
print(text1[int_digit-1])

Not really much to explain here, it's just how python works.

You need the -1 at the end, because this function grabs by index (which starts at 0)

Upvotes: 2

Related Questions