Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 7th, 2012, 10:52 PM
sivasubramanian sivasubramanian is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 sivasubramanian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old November 7th, 2012, 11:10 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,352 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 49 m 23 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old November 7th, 2012, 11:17 PM
sivasubramanian sivasubramanian is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 sivasubramanian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old November 7th, 2012, 11:34 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,352 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 49 m 23 sec
Reputation Power: 383
'ponmlkjihg'

10 == len( 'ponmlkjihg' )

Reply With Quote
  #5  
Old November 7th, 2012, 11:37 PM
sivasubramanian sivasubramanian is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 sivasubramanian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old November 7th, 2012, 11:48 PM
sivasubramanian sivasubramanian is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 sivasubramanian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #7  
Old November 7th, 2012, 11:58 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,352 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 49 m 23 sec
Reputation Power: 383
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.

Reply With Quote
  #8  
Old November 8th, 2012, 12:03 AM
sivasubramanian sivasubramanian is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 sivasubramanian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!!!

Reply With Quote
  #9  
Old November 10th, 2012, 05:20 PM
baski11 baski11 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 baski11 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #10  
Old November 10th, 2012, 07:25 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,352 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 49 m 23 sec
Reputation Power: 383
You posted a syntax error. (Bad quotes, in my opinion.) What do you mean?

Reply With Quote
  #11  
Old November 10th, 2012, 07:39 PM
baski11 baski11 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 baski11 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 25 sec
Reputation Power: 0
Unhappy

That's correct...I have reposted.

Reply With Quote
  #12  
Old November 10th, 2012, 08:33 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,352 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 49 m 23 sec
Reputation Power: 383
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.

Reply With Quote
  #13  
Old November 10th, 2012, 10:07 PM
baski11 baski11 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 baski11 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 25 sec
Reputation Power: 0
Thanks very much. Appreciate your time.
Will try this code. Thanks...

Reply With Quote
  #14  
Old November 11th, 2012, 08:55 AM
baski11 baski11 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 baski11 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #15  
Old November 11th, 2012, 04:58 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,352 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 49 m 23 sec
Reputation Power: 383
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Print 10 charaters from right in python

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap