The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Print 10 charaters from right in python
Discuss Print 10 charaters from right in python in the Python Programming forum on Dev Shed. Print 10 charaters from right in python Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 7th, 2012, 10:52 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
Time spent in forums: 1 h 17 m 17 sec
Reputation Power: 0
|
|
|
Print 10 charaters from right in python
Hi,
I want to print 10 characters from right.
e.g. name="abcdef123456789asd"
i should be able to get o/p as "dsa9876543"
I tried the below method:
name = "abcdef123456789asd"
reverse= name[::-1] # This reverses the entire string
name=reverse[0:10]
print name
but believe there should be some other shorter way of doing it.
Request you to pls help.
Thanks
Siva
|

November 7th, 2012, 11:10 PM
|
 |
Contributing User
|
|
|
|
Code:
>>> 'abcdefghijklmnop'[-1:-11:-1]
'ponmlkjihg'
>>> len('abcdefghijklmnop'[-1:-11:-1])
10
>>> reversed('123')
<reversed object at 0xd32dd0>
>>> list(reversed('123'))
['3', '2', '1']
>>>
__________________
[code] Code tags[/code] are essential for python code!
|

November 7th, 2012, 11:17 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
Time spent in forums: 1 h 17 m 17 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by b49P23TIvg
Code:
>>> 'abcdefghijklmnop'[-1:-11:-1]
'ponmlkjihg'
>>> len('abcdefghijklmnop'[-1:-11:-1])
10
>>> reversed('123')
<reversed object at 0xd32dd0>
>>> list(reversed('123'))
['3', '2', '1']
>>>
|
hi b49P23TIvg,
Thanks for your reply.. it works fine... can you pls explain how does this [-1:-11:-1] calculate to 10.
Thanks,
Siva
|

November 7th, 2012, 11:34 PM
|
 |
Contributing User
|
|
|
|
|
'ponmlkjihg'
10 == len( 'ponmlkjihg' )
|

November 7th, 2012, 11:37 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
Time spent in forums: 1 h 17 m 17 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by b49P23TIvg 'ponmlkjihg'
10 == len( 'ponmlkjihg' ) |
I understood it.. Thanks.
|

November 7th, 2012, 11:48 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
Time spent in forums: 1 h 17 m 17 sec
Reputation Power: 0
|
|
|
Printing characters
I am new to python and trying out different things
a="abcdefghijklmnopqrst"
I want to print the last 10 characters i.e. klmnopqrst .. How do i do this?
Please let me know. Also , request you to suggest a good book for understanding python
Thanks
Siva
|

November 7th, 2012, 11:58 PM
|
 |
Contributing User
|
|
|
|
Code:
>>> #please try lots of slice tests on your own in the python interpreter
>>> A=list(range(20))
>>> A[1::2] # odd
>>> A[::2] # objects at even indexes
>>> A[:10] # first 10 objects
>>> A[10:14] # objects 10, 11, 12, and 13
www.python.org find the tutorial. Read it and experiment in the python interpreter as you go.
|

November 8th, 2012, 12:03 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
Time spent in forums: 1 h 17 m 17 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by b49P23TIvg
Code:
>>> #please try lots of slice tests on your own in the python interpreter
>>> A=list(range(20))
>>> A[1::2] # odd
>>> A[::2] # objects at even indexes
>>> A[:10] # first 10 objects
>>> A[10:14] # objects 10, 11, 12, and 13
www.python.org find the tutorial. Read it and experiment in the python interpreter as you go. |
Thanks mate!!!
|

November 10th, 2012, 05:20 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
Time spent in forums: 2 h 25 sec
Reputation Power: 0
|
|
|
LIST - search
Can you try this?
I have a list that says
Now I want to loop thru this and compare a value in a variable with the value of num in the above list. Any ideas??
Like my var may have 004, I need to check this against 'num' in the above list....
Thanks for the reply.... I think it should be
[('key1=1,t=US', {'Num': ['004'],'name': ['Roger'],'locCity': ['Oakbrook']}),
('key1=2,t=US', {'Num': ['005'],'name': ['Din'],'locCity': ['Brookside']})
]
Yes...Hope it makes sense now....
Thanks.
|

November 10th, 2012, 07:25 PM
|
 |
Contributing User
|
|
|
|
|
You posted a syntax error. (Bad quotes, in my opinion.) What do you mean?
|

November 10th, 2012, 07:39 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
Time spent in forums: 2 h 25 sec
Reputation Power: 0
|
|
That's correct...I have reposted.
|

November 10th, 2012, 08:33 PM
|
 |
Contributing User
|
|
|
|
Code:
>>> var = '004'
>>> import pprint
>>> pprint.pprint(A)
[('key1=1,t=US', {'Num': ['004'], 'locCity': ['Oakbrook'], 'name': ['Roger']}),
('key1=2,t=US', {'Num': ['005'], 'locCity': ['Brookside'], 'name': ['Din']})]
>>> for L in A:
... D = L[1]
... for value in D.values():
... if var in value:
... print('found')
>>>
When structures get convoluted so that you need specialized functions to extract data you might do better with a class.
|

November 10th, 2012, 10:07 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
Time spent in forums: 2 h 25 sec
Reputation Power: 0
|
|
|
Thanks very much. Appreciate your time.
Will try this code. Thanks...
|

November 11th, 2012, 08:55 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
Time spent in forums: 2 h 25 sec
Reputation Power: 0
|
|
|
From the following LIST, I need to create a LIST or DICTIONARY where I need few fields from each row based on a match. Like I will get only 1 match at a time.
[('key1=1,t=US',
{'Num': ['004'], 'locCity': 'Oakbrook'], 'name': ['Roger'],'uid': [lname.fname@msn.com],'Date': [101010]}),
('key1=2,t=US',
{'Num': ['005'], 'locCity': ['Brookside'], 'name': ['Din'],'uid':[lname1.fname1@msn.com],'Date': [101110]}),
('key1=3,t=US',
{'Num': ['006'], 'locCity': ['OakBrook'], 'name': ['Dan'],'uid':[lname2.fname2@msn.com],'Date': [111010]})]
I have a string with lastname.firstname. With this I should loop thru the above tuples and for the matching entry, I have to store keys like Num, locCity and Date and their values. So I believe it should be a dictionary?.
Could it be done? Please advise.
|

November 11th, 2012, 04:58 PM
|
 |
Contributing User
|
|
|
|
|
You ignored my "design a class for complicated nests of lists and dictionaries" advice. Perhaps start a new thread to get someone else's opinion.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|