Re: [Tutor] string reversal using [::-1]

2017-06-12 Thread Alan Gauld via Tutor
On 12/06/17 15:57, Vikas YADAV wrote: > for i in range(len(s)-1, 1, -2): > print s[i] > - > > So my question is: how would you write "s[::-1]" in terms of a for loop for > illustration purpose? Exactly as above but replace -2 with -1 for i in ran

Re: [Tutor] string reversal using [::-1]

2017-06-12 Thread Vikas YADAV
.pyt...@gmail.com] Sent: Monday, June 12, 2017 1:26 AM To: Vikas YADAV Cc: tutor Subject: Re: [Tutor] string reversal using [::-1] [QUOTED ENTIRELY FROM STEVE {st...@pearwood.info} IN ANSWER TO A SLICING QUESTION] The way to think about string indexing and slicing is that the index positions

Re: [Tutor] string reversal using [::-1]

2017-06-12 Thread Abdur-Rahmaan Janhangeer
[QUOTED ENTIRELY FROM STEVE {st...@pearwood.info} IN ANSWER TO A SLICING QUESTION] The way to think about string indexing and slicing is that the index positions mark *between* the characters. Take your string: Machine learning is awesome! For brevity, I'll just use the first word: Mach

Re: [Tutor] string reversal using [::-1]

2017-06-11 Thread Peter Otten
Vikas YADAV wrote: > Question: Why does "123"[::-1] result in "321"? > > > > MY thinking is [::-1] is same as [0:3:-1], that the empty places defaults > to start and end index of the string object. > > So, if we start from 0 index and decrement index by 1 till we reach 3, how > many index we

Re: [Tutor] string reversal using [::-1]

2017-06-10 Thread Alan Gauld via Tutor
On 10/06/17 17:39, Vikas YADAV wrote: > Question: Why does "123"[::-1] result in "321"? > > MY thinking is [::-1] is same as [0:3:-1], that the empty places defaults to > start and end index of the string object. Did you try that? You may be surprised. The wonderful thing about the >>> prompt is

Re: [Tutor] String within a string solution (newbie question)

2016-10-27 Thread Matt Ruffalo
On 10/26/2016 02:06 PM, Wish Dokta wrote: > Hello, > > I am currently writing a basic program to calculate and display the size of > folders with a drive/directory. To do this I am storing each directory in a > dict as the key, with the value being the sum of the size of all files in > that directo

Re: [Tutor] String within a string solution (newbie question)

2016-10-27 Thread Bob Gailer
BubOn Oct 27, 2016 8:38 AM, "Wish Dokta" wrote: > > Hello Alan, > > Thank you for the reply. > > I have actually fixed that bug. If you are bored or for some other reason > would like to assist a newbro my code is here: > > main: http://pastebin.com/LgbeywiB > functions: http://pastebin.com/vU7zzJ

Re: [Tutor] String within a string solution (newbie question)

2016-10-27 Thread Wish Dokta
Thank you Bob, While you were correct adding "\\" helped, I also needed to add "\\" to the dict key so it would also pic up the root directory. Many thanks, Glen On 26 October 2016 at 19:43, Bob Gailer wrote: > On Oct 26, 2016 2:07 PM, "Wish Dokta" wrote: > > > > Hello, > > > > I am currently

Re: [Tutor] String within a string solution (newbie question)

2016-10-27 Thread Wish Dokta
Hello Alan, Thank you for the reply. I have actually fixed that bug. If you are bored or for some other reason would like to assist a newbro my code is here: main: http://pastebin.com/LgbeywiB functions: http://pastebin.com/vU7zzJKe I'd be very grateful for any feedback on improvements to the c

Re: [Tutor] String within a string solution (newbie question)

2016-10-26 Thread Bob Gailer
On Oct 26, 2016 2:07 PM, "Wish Dokta" wrote: > > Hello, > > I am currently writing a basic program to calculate and display the size of > folders with a drive/directory. To do this I am storing each directory in a > dict as the key, with the value being the sum of the size of all files in > that d

Re: [Tutor] String within a string solution (newbie question)

2016-10-26 Thread Alan Gauld via Tutor
On 26/10/16 19:06, Wish Dokta wrote: > folders with a drive/directory. To do this I am storing each directory in a > dict as the key, with the value being the sum of the size of all files in > that directories (but not directories). > > For example: > > for "C:\\docs\\code" in key: > > Which wo

Re: [Tutor] String Attribute

2015-08-02 Thread Alan Gauld
On 02/08/15 10:15, Ltc Hotspot wrote: Question1: Why did the following strip function fail: line2 = line.strip (',') What makes you think it failed? I see no error messages below. Question2: How do I code a vertical column output See below. Revised code: fname = raw_input("Enter f

Re: [Tutor] String Attribute

2015-08-02 Thread Ltc Hotspot
Hi Alan, Question1: Why did the following strip function fail: line2 = line.strip (',') View instructions for 'str.strip([*chars*])¶ ' which is available at https://docs.pythonorg/2.7/library/stdtypes.html?highlig

Re: [Tutor] String Attribute

2015-08-02 Thread Alan Gauld
On 02/08/15 02:20, Ltc Hotspot wrote: Hi Alan, I made a mistake and incorrectly assumed that differences between 54 lines of output and 27 lines of output is the result of removing duplicate email addresses, Apparently, this is not the case and I was wrong :( The solution to the problem is in t

Re: [Tutor] String Attribute

2015-08-02 Thread Ltc Hotspot
Hi Emile, I made a mistake and incorrectly assumed that differences between 54 lines of output and 27 lines of output is the result of removing duplicate email addresses, i.e., gsil...@umich.edu gsil...@umich.edu, c...@iupui.edu, c...@iupui.edu Apparently, this is not the case and I was wrong :(

Re: [Tutor] String Attribute

2015-08-02 Thread Ltc Hotspot
Hi Alan, I made a mistake and incorrectly assumed that differences between 54 lines of output and 27 lines of output is the result of removing duplicate email addresses, i.e., gsil...@umich.edu gsil...@umich.edu, c...@iupui.edu, c...@iupui.edu Apparently, this is not the case and I was wrong :(

Re: [Tutor] String Attribute

2015-08-01 Thread Emile van Sebille
On 8/1/2015 4:21 PM, Ltc Hotspot wrote: Hi Emile, Question: What is the source of the line 7 syntax: mbox.split? I read mbox from the file. eg, mbox = open("mbox-short.txt",'r').read() and it looks to me that if you insert the above in front of the for loop below you'll get further. Emil

Re: [Tutor] String Attribute

2015-08-01 Thread Emile van Sebille
On 8/1/2015 4:07 PM, Ltc Hotspot wrote: Hi Alan, Question1: The output result is an address or line? It's a set actually. Ready to be further processed I imagine. Or to print out line by line if desired. Question2: Why are there 54 lines as compared to 27 line in the desired output? Be

Re: [Tutor] String Attribute

2015-08-01 Thread Alan Gauld
On 02/08/15 00:07, Ltc Hotspot wrote: Question1: The output result is an address or line? Its your assignment,. you tell me. But from your previous mails I'm assuming you want addresses? Question2: Why are there 54 lines as compared to 27 line in the desired output? Because the set removes

Re: [Tutor] String Attribute

2015-08-01 Thread Ltc Hotspot
Hi Alan, Question1: The output result is an address or line? Question2: Why are there 54 lines as compared to 27 line in the desired output? Here is the latest revised code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 addresses =

Re: [Tutor] String Attribute

2015-08-01 Thread Ltc Hotspot
Hi Emile, Question: What is the source of the line 7 syntax: mbox.split? Here is a copy of the Traceback message: NameError Traceback (most recent call last) C:\Users\vm\Desktop\apps\docs\Python\8_5_v_26.py in () 5 addresses = set() 6 for addr in [ fromline.split()[0] > 7 for f

Re: [Tutor] String Attribute

2015-08-01 Thread Emile van Sebille
On 8/1/2015 12:00 PM, Ltc Hotspot wrote: Hi Everyone: Let me repost the question: You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out a count at the end. *Hint:* make sure not to incl

Re: [Tutor] String Attribute

2015-08-01 Thread Alan Gauld
On 01/08/15 19:48, Ltc Hotspot wrote: There is an indent message in the revised code. Question: Where should I indent the code line for the loop? Do you understand the role of indentation in Python? Everything in the indented block is part of the structure, so you need to indent everything that

Re: [Tutor] String Attribute

2015-08-01 Thread Ltc Hotspot
Hi Everyone: Let me repost the question: You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out a count at the end. *Hint:* make sure not to include the lines that start with 'From:'. You

Re: [Tutor] String Attribute

2015-08-01 Thread Ltc Hotspot
Hi Emile, I just noticed there are duplicates Here is the complete line output as requested, below: stephen.marqu...@uct.ac.za lo...@media.berkeley.edu zq...@umich.edu rjl...@iupui.edu zq...@umich.edu rjl...@iupui.edu c...@iupui.edu c...@iupui.edu gsil...@umich.edu gsil...@umich.edu zq...@umich

Re: [Tutor] String Attribute

2015-08-01 Thread Ltc Hotspot
Hi Alan, There is an indent message in the revised code. Question: Where should I indent the code line for the loop? View the revised codes with loop indents, below: --->Revised Code v.2 wo/indent from lines 8-12: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.t

Re: [Tutor] String Attribute

2015-08-01 Thread Emile van Sebille
Hi Hal, Seeing now that the output is only extracted from six address blocks, can you paste in the full contents of the file mbox-short.txt? (or the first 5-10 address sets if this is only representative) I think if we have a better understanding of the structure of the content you're parsin

Re: [Tutor] String Attribute

2015-08-01 Thread Cameron Simpson
On 31Jul2015 17:21, Ltc Hotspot wrote: Mark: Is this any better, message sent from GMail? Regards, Hal Looks better to me. Cheers, Cameron Simpson On Fri, Jul 31, 2015 at 5:02 PM, Mark Lawrence wrote: On 31/07/2015 19:57, ltc.hots...@gmail.com wrote: I believe that this is the third ti

Re: [Tutor] String Attribute

2015-08-01 Thread Alan Gauld
On 01/08/15 00:59, ltc.hots...@gmail.com wrote: for line in fh: line2 = line.strip() line3 = line2.split() line4 = line3[0] →→Apparently, the data content in the file is lost from the address sort function to line2? : It is not lost, it is an empty line. In [47]: print line2.sp

Re: [Tutor] String Attribute

2015-08-01 Thread Ltc Hotspot
Mark: Is this any better, message sent from GMail? Regards, Hal On Fri, Jul 31, 2015 at 5:02 PM, Mark Lawrence wrote: > On 31/07/2015 19:57, ltc.hots...@gmail.com wrote: > > I believe that this is the third time that you've been asked to do > something about the amount of whitespace that you're

Re: [Tutor] String Attribute

2015-08-01 Thread Ltc Hotspot
Hi Mark, Desired output on execution of the script: stephen.marqu...@uct.ac.za lo...@media.berkeley.edu zq...@umich.edu rjl...@iupui.edu zq...@umich.edu rjl...@iupui.edu [...] Regards, Hal On Fri, Jul 31, 2015 at 5:21 PM, Ltc Hotspot wrote: > Mark: > Is this any better, message sent from G

Re: [Tutor] String Attribute

2015-08-01 Thread ltc.hotspot
Sent from Surface From: Alan Gauld Sent: ‎Friday‎, ‎July‎ ‎31‎, ‎2015 ‎4‎:‎54‎ ‎PM To: Tutor@python.org On 31/07/15 19:57, ltc.hots...@gmail.com wrote: > for line in fh: > line2 = line.strip() > line3 = line2.split() > line4 = line3[0] You need to check that there actually

Re: [Tutor] String Attribute

2015-07-31 Thread Mark Lawrence
On 31/07/2015 19:57, ltc.hots...@gmail.com wrote: I believe that this is the third time that you've been asked to do something about the amount of whitespace that you're sending to this list. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our langua

Re: [Tutor] String Attribute

2015-07-31 Thread ltc.hotspot
Emile, --> Captured is a printout from line3 to addresses, below: In [46]: print line3 [] In [47]: print line2.split() [] In [48]: print line2 In [49]: print line.strip() In [50]: print fh In [51]: print addresses set(['1.0', 'sou...@collab.sakaiproject.org;', 'Jan', 'mail.umich.ed

Re: [Tutor] String Attribute

2015-07-31 Thread Alan Gauld
On 31/07/15 19:57, ltc.hots...@gmail.com wrote: for line in fh: line2 = line.strip() line3 = line2.split() line4 = line3[0] You need to check that there actually is something in the list to access. If you get a line with only one word in it, or even a blank line this will fail. addr

Re: [Tutor] String Attribute

2015-07-31 Thread Emile van Sebille
On 7/31/2015 11:57 AM, ltc.hots...@gmail.com wrote: →Question: Why is the list index out of range on line # 9: IndexError Traceback (most recent call last) C:\Users\vm\Desktop\apps\docs\Python\assinment_8_5_v_20.py in () 7 line2 = line.strip() 8 line3 = line2.s

Re: [Tutor] String Attribute

2015-07-31 Thread ltc.hotspot
Hi Martin, Hal is not have a great day, indeed to day: Here is the raw data entered: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 addresses = set() for line in fh: line2 = line.strip() line3 = line2.split() line4 = line3[

Re: [Tutor] String Attribute

2015-07-31 Thread Martin A. Brown
Greetings again Hal, Thank you for posting your small amounts of code and results inline. Thanks for also including clear questions. Your "surface" still seems to add extra space, so, if you could trim that, you may get even more responses from others who are on the Tutor mailing list. Now

Re: [Tutor] String Attribute

2015-07-31 Thread Alan Gauld
On 31/07/15 15:39, ltc.hots...@gmail.com wrote: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1]

Re: [Tutor] String Attribute

2015-07-31 Thread ltc.hotspot
Hi Alan, Here is the revised code below: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1]

Re: [Tutor] String Attribute

2015-07-31 Thread Alan Gauld
On 31/07/15 01:25, ltc.hots...@gmail.com wrote: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1]

Re: [Tutor] String Attribute

2015-07-31 Thread ltc.hotspot
Hi Alan, I rewrote the code as follows: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = lin

Re: [Tutor] String Attribute

2015-07-30 Thread Mark Lawrence
On 30/07/2015 23:34, ltc.hots...@gmail.com wrote: sure Sent from Surface From: Mark Lawrence Sent: ‎Thursday‎, ‎July‎ ‎30‎, ‎2015 ‎3‎:‎25‎ ‎PM To: Tutor@python.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscri

Re: [Tutor] String Attribute

2015-07-30 Thread Alan Gauld
On 30/07/15 23:51, ltc.hots...@gmail.com wrote: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" # assign fname fh=open(fname,'r') # Open a new file handle for line in fh: print line if 'From' in line.split()[0] and '@' in line: sender = line.split()[

Re: [Tutor] String Attribute

2015-07-30 Thread Alan Gauld
On 30/07/15 22:17, ltc.hots...@gmail.com wrote: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" # assign fname fh=open(fname,'r') # Open a new file handle for line in fh: print line if 'From' in line.split()[0] and '@' in line: sender = line.split()[

Re: [Tutor] String Attribute

2015-07-30 Thread ltc.hotspot
Hi Mark, I’m still confused because line 4 reads: fh=open(fname,'r') # Open a new file handle, not fn = open(fname) Therefore, can you write down line by line from error to correction? Here is the revised code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-shor

Re: [Tutor] String Attribute

2015-07-30 Thread ltc.hotspot
sure Sent from Surface From: Mark Lawrence Sent: ‎Thursday‎, ‎July‎ ‎30‎, ‎2015 ‎3‎:‎25‎ ‎PM To: Tutor@python.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listi

Re: [Tutor] String Attribute

2015-07-30 Thread ltc.hotspot
Hi everyone, Revised code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" # assign fname fh=open(fname,'r') # Open a new file handle for line in fh: print line if 'From' in line.split()[0] and '@' in line: sender = line.split()[1] fn.seek

Re: [Tutor] String Attribute

2015-07-30 Thread Mark Lawrence
On 30/07/2015 20:07, ltc.hots...@gmail.com wrote: When you post here can you please find a mechanism that gives us more text than whitespace, thank you. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence

Re: [Tutor] String Attribute

2015-07-30 Thread ltc.hotspot
Sent from Surface From: ltc.hots...@gmail.com Sent: ‎Thursday‎, ‎July‎ ‎30‎, ‎2015 ‎11‎:‎47‎ ‎AM To: Steven D'Aprano Hi Steve: New revision code: count = 0 fn = raw_input("Enter file name: ") if len(fn) < 1 : fname = "mbox-short.txt" for line in fn: if 'From' in line.spl

Re: [Tutor] String Attribute

2015-07-29 Thread Steven D'Aprano
On Tue, Jul 28, 2015 at 11:33:53PM +, ltc.hots...@gmail.com wrote: > > Hi Everyone: > > What is the source of the syntax error to the String Attribute? > > Go to the following URL links and view a copy of the raw data file code and > sample data: Please don't send people to URLs to view y

Re: [Tutor] String Attribute

2015-07-29 Thread Alan Gauld
On 29/07/15 00:33, ltc.hots...@gmail.com wrote: Hi Everyone: What is the source of the syntax error to the String Attribute? Normally I'd ask you to post the full text of any errors. They usually contain a lot of useful information. They also help us identify which syntax error you are askin

Re: [Tutor] String Problem

2015-07-06 Thread Cameron Simpson
On 06Jul2015 15:44, Crusier wrote: Dear All, I have used the urllib.request and download some of the information from a site. I am currently using Python 3.4. My program is as follows: import urllib.request response = urllib.request.urlopen(' http://www.hkex.com.hk/eng/ddp/Contract_Details.a

Re: [Tutor] String Problem

2015-07-06 Thread Mark Lawrence
On 06/07/2015 08:44, Crusier wrote: Dear All, I have used the urllib.request and download some of the information from a site. I am currently using Python 3.4. My program is as follows: import urllib.request response = urllib.request.urlopen(' http://www.hkex.com.hk/eng/ddp/Contract_Details.a

Re: [Tutor] string delimiters

2015-06-03 Thread Cameron Simpson
On 03Jun2015 17:35, richard kappler wrote: Perhaps the better way for me to have asked this question would have been: How can I find the location within a string of every instance of a character such as ']'? With the str.find method! s = 'a]b]c' pos = s.find(']') while pos >= 0: print(

Re: [Tutor] string delimiters

2015-06-03 Thread Cameron Simpson
On 03Jun2015 14:16, Alex Kleider wrote: On 2015-06-03 12:53, Alan Gauld wrote: ... If this is really about parsing dates and times have you looked at the datetime module and its parsing/formatting functions (ie strptime/strftime)? I asssume strftime gets its name from 'string from time.' What

Re: [Tutor] string delimiters

2015-06-03 Thread Alan Gauld
On 03/06/15 22:16, Alex Kleider wrote: On 2015-06-03 12:53, Alan Gauld wrote: ... If this is really about parsing dates and times have you looked at the datetime module and its parsing/formatting functions (ie strptime/strftime)? I asssume strftime gets its name from 'string from time.' What a

Re: [Tutor] string delimiters

2015-06-03 Thread Alex Kleider
On 2015-06-03 15:13, Mark Lawrence wrote: 'f' for format, 'p' for parse, having originally come from plain old C. More here https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior So I was wrong about the 'f' as well as having no clue about the 'p'! Thank you very much

Re: [Tutor] string delimiters

2015-06-03 Thread Mark Lawrence
On 03/06/2015 22:16, Alex Kleider wrote: On 2015-06-03 12:53, Alan Gauld wrote: ... If this is really about parsing dates and times have you looked at the datetime module and its parsing/formatting functions (ie strptime/strftime)? I asssume strftime gets its name from 'string from time.' What

Re: [Tutor] string delimiters

2015-06-03 Thread Peter Otten
richard kappler wrote: > Perhaps the better way for me to have asked this question would have been: > > How can I find the location within a string of every instance of a > character such as ']'? >>> import re >>> s = "alpha]beta]gamma]delta" >>> [m.start() for m in re.finditer(r"]", s)] [5, 10,

Re: [Tutor] string delimiters

2015-06-03 Thread richard kappler
Perhaps the better way for me to have asked this question would have been: How can I find the location within a string of every instance of a character such as ']'? regards, Richard On Wed, Jun 3, 2015 at 5:16 PM, Alex Kleider wrote: > On 2015-06-03 12:53, Alan Gauld wrote: > ... > >> If this

Re: [Tutor] string delimiters

2015-06-03 Thread Alex Kleider
On 2015-06-03 12:53, Alan Gauld wrote: ... If this is really about parsing dates and times have you looked at the datetime module and its parsing/formatting functions (ie strptime/strftime)? I asssume strftime gets its name from 'string from time.' What about strptime? How did that get its name

Re: [Tutor] string delimiters

2015-06-03 Thread richard kappler
figured that out from your last post, and thank you, now I understand how that works. I thought I was looking for the entire string, not each character. That bit all makes sense now. A descriptor is, for example, for the following part of a string '0032.4' the descriptor would be weight, so the fo

Re: [Tutor] string delimiters

2015-06-03 Thread Alan Gauld
On 03/06/15 21:13, richard kappler wrote: I was trying to keep it simple, you'd think by now I'd know better. My fault and my apology. It's definitely not all dates and times, the data and character types vary. This is the output from my log parser script which you helped on the other day. t

Re: [Tutor] string delimiters

2015-06-03 Thread Alan Gauld
On 03/06/15 21:23, richard kappler wrote: hold the phone I have no idea why it worked, would love an explanation, but I changed my previous test script by eliminating for tag in ("icdm"): This loops over the string assigning the characters i,c,d and m to tag if 'icdm' in line: This

Re: [Tutor] string delimiters

2015-06-03 Thread richard kappler
hold the phone I have no idea why it worked, would love an explanation, but I changed my previous test script by eliminating for tag in ("icdm"): and changing if tag in line to if 'icdm' in line: and it works perfectly! It only iterates over the file once, and the else executes so both t

Re: [Tutor] string delimiters

2015-06-03 Thread richard kappler
I was trying to keep it simple, you'd think by now I'd know better. My fault and my apology. It's definitely not all dates and times, the data and character types vary. This is the output from my log parser script which you helped on the other day. there are essentially two types of line: Tue Ju

Re: [Tutor] string delimiters

2015-06-03 Thread Alan Gauld
On 03/06/15 20:10, richard kappler wrote: for formatting a string and adding descriptors: test = 'datetimepart1part2part3the_rest' If this is really about parsing dates and times have you looked at the datetime module and its parsing/formatting functions (ie strptime/strftime)? Can I stop us

Re: [Tutor] String method "strip()" not working

2015-03-07 Thread Alan Gauld
On 07/03/15 15:17, Mark Lawrence wrote: S.strip([chars]) -> string or unicode Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unic

Re: [Tutor] String method "strip()" not working

2015-03-07 Thread Mark Lawrence
On 07/03/2015 14:54, Alan Gauld wrote: On 07/03/15 13:15, Akash Shekhar wrote: I am trying to learn how to use strip() method. It is supposed to cut out all the whitespace as I read in the tutorial. Read it again more closely. --- Help on built-in function strip: strip(...)

Re: [Tutor] String method "strip()" not working

2015-03-07 Thread Dave Angel
On 03/07/2015 08:15 AM, Akash Shekhar wrote: I am trying to learn how to use strip() method. It is supposed to cut out all the whitespace as I read in the tutorial. But the code is not working. Here's my code: sentence = "Hello, how are you?" print(sentence) print(sentence.strip())

Re: [Tutor] String method "strip()" not working

2015-03-07 Thread Alan Gauld
On 07/03/15 13:15, Akash Shekhar wrote: I am trying to learn how to use strip() method. It is supposed to cut out all the whitespace as I read in the tutorial. Read it again more closely. --- Help on built-in function strip: strip(...) S.strip([chars]) -> string or unicode

Re: [Tutor] String with literal %s

2014-03-20 Thread Steven D'Aprano
On Thu, Mar 20, 2014 at 04:18:35PM +, Alan Gauld wrote: > On 20/03/14 14:46, Toni Fuente wrote: > > >I am trying to create a string this way: > > > >insertion = "INSERT INTO mytable(week %s) VALUES (\%s, \%s)" % osStringI Don't do this! The world has far too many software vulnerable to code

Re: [Tutor] String with literal %s

2014-03-20 Thread Toni Fuente
* Alan Gauld [2014-03-20 16:18:35 +]: > On 20/03/14 14:46, Toni Fuente wrote: > > >I am trying to create a string this way: > > > >insertion = "INSERT INTO mytable(week %s) VALUES (\%s, \%s)" % osStringI > > > >not enough arguments for format string > > > >Where the first %s is going to be s

Re: [Tutor] String with literal %s

2014-03-20 Thread Toni Fuente
* Alex Kleider [2014-03-20 09:02:41 -0700]: > On 2014-03-20 07:46, Toni Fuente wrote: > >Hi again, > > > >I am trying to create a string this way: > > > >insertion = "INSERT INTO mytable(week %s) VALUES (\%s, \%s)" % > >osStringI > > > >not enough arguments for format string > > > >Where the firs

Re: [Tutor] String with literal %s

2014-03-20 Thread Alan Gauld
On 20/03/14 14:46, Toni Fuente wrote: I am trying to create a string this way: insertion = "INSERT INTO mytable(week %s) VALUES (\%s, \%s)" % osStringI not enough arguments for format string Where the first %s is going to be substitute by the variable osStringI, but the other two VALUES (\%s,

Re: [Tutor] String with literal %s

2014-03-20 Thread Alex Kleider
On 2014-03-20 07:46, Toni Fuente wrote: Hi again, I am trying to create a string this way: insertion = "INSERT INTO mytable(week %s) VALUES (\%s, \%s)" % osStringI not enough arguments for format string Where the first %s is going to be substitute by the variable osStringI, but the other tw

Re: [Tutor] string indexing -- side note, rather OT

2014-01-20 Thread spir
On 01/20/2014 01:19 AM, Keith Winston wrote: On Sun, Jan 19, 2014 at 3:50 PM, Alan Gauld wrote: How would Python know whether you want find for gettext, mmap, str, xml.etree.ElementTree.Element or xml.etree.ElementTree.ElementTree? Absolutely, but a newbie doesn't even guess that more than o

Re: [Tutor] string indexing

2014-01-20 Thread Alan Gauld
On 20/01/14 07:37, rahmad akbar wrote: Spir and Peter, thanks for the specifics, super helpful. Alan, super thanks for the general advice, you guys are awesome!! You are welcome, but please don't post an entire digest just to say thanks. It uses up bandwidth and storage unnecessarily and some

Re: [Tutor] string indexing

2014-01-20 Thread rahmad akbar
ng text tends to lead programmers > to be verbally based. But algorithms, state machines, logic, data > structures, GUIs, formal requirements and OOP all have well > established visual representations, and in many cases they > are formalized so that, with the right tools, you c

Re: [Tutor] string indexing

2014-01-19 Thread Keith Winston
On Sun, Jan 19, 2014 at 3:50 PM, Alan Gauld wrote: >> How would Python know whether you want find for gettext, mmap, str, >> xml.etree.ElementTree.Element or xml.etree.ElementTree.ElementTree? > > > Absolutely, but a newbie doesn't even guess that more than one find would > exist. Or even that the

Re: [Tutor] string indexing

2014-01-19 Thread Alan Gauld
On 19/01/14 19:53, Mark Lawrence wrote: On 19/01/2014 19:34, Keith Winston wrote: Erm, getting what you want from help can be work. Help(find) # doesn't work at all. How would Python know whether you want find for gettext, mmap, str, xml.etree.ElementTree.Element or xml.etree.ElementTree

Re: [Tutor] string indexing

2014-01-19 Thread Mark Lawrence
On 19/01/2014 19:34, Keith Winston wrote: On Sun, Jan 19, 2014 at 11:33 AM, Alan Gauld wrote: help(''.find) Help on built-in function find: Erm, getting what you want from help can be work. Help(find) # doesn't work at all. How would Python know whether you want find for gettext, mmap,

Re: [Tutor] string indexing

2014-01-19 Thread Keith Winston
On Sun, Jan 19, 2014 at 11:33 AM, Alan Gauld wrote: help(''.find) > Help on built-in function find: Erm, getting what you want from help can be work. Help(find) # doesn't work at all. What Alan did above was create an empty string, by using two single quotes next to each other ('', not t

Re: [Tutor] string indexing

2014-01-19 Thread Alan Gauld
On 19/01/14 13:59, rahmad akbar wrote: hey guys, super noob here, i am trying to understand the following code from google tutorial which i failed to comprehend Others have answered the specifics but some general advice here: 1) never forget the Python >>> prompt. Try things out if you don't

Re: [Tutor] string indexing

2014-01-19 Thread Peter Otten
rahmad akbar wrote: > hey guys, super noob here, i am trying to understand the following code > from google tutorial which i failed to comprehend > > #code start > # E. not_bad > # Given a string, find the first appearance of the > # substring 'not' and 'bad'. If the 'bad' follows > # the 'not',

Re: [Tutor] string indexing

2014-01-19 Thread spir
On 01/19/2014 02:59 PM, rahmad akbar wrote:> hey guys, super noob here, i am trying to understand the following code from google tutorial which i failed to comprehend #code start # E. not_bad # Given a string, find the first appearance of the # substring 'not' and 'bad'. If the 'bad' follows #

Re: [Tutor] string codes

2013-11-28 Thread Steven D'Aprano
On Wed, Nov 27, 2013 at 10:20:03PM +0100, spir wrote: > On 11/26/2013 12:59 PM, Steven D'Aprano wrote: > >On Tue, Nov 26, 2013 at 10:01:14AM +, Alan Gauld wrote: > > > >>>Is there a method to compare a substring, without building a substring > >>>from the big one? Like startswith or endswith, b

Re: [Tutor] string codes

2013-11-28 Thread spir
On 11/28/2013 02:34 AM, Alan Gauld wrote: Not so. If you are looking for a string and know the string ends with that string you want the end point to exclude the known result at the end. And it is a startswith because you are checking from the start of the substring. Ah, thank you, Alan! Denis

Re: [Tutor] string codes

2013-11-28 Thread spir
On 11/28/2013 02:12 AM, Walter Prins wrote: Sorry to wade in after all the other answers you've had, but a) string.find() does not *require* start and end indexes, they are optional: http://docs.python.org/2/library/string.htmlAnd b) if you're just trying to find out whether a substring exist

Re: [Tutor] string codes

2013-11-27 Thread Alan Gauld
On 27/11/13 21:20, spir wrote: py> s.startswith("bcd", 1, -1) and s.endswith("bcd", 1, -1) True Hum, I don't understand the reasoning, here. * First, why use the end-index param? (Here in this case, or in any other)? It contradicts the idea of starting with in my view, but also is useless for

Re: [Tutor] string codes

2013-11-27 Thread Walter Prins
Hi, On 27 November 2013 21:20, spir wrote: > All in all, startswith plus start-index only seems to work fine, I guess. > What is wrong? string.find also works (someone suggested it on the > python-ideas mailing list) but requires both start- and end- indexes. Also, > startswith returns true/fal

Re: [Tutor] string codes

2013-11-27 Thread spir
On 11/26/2013 12:59 PM, Steven D'Aprano wrote: On Tue, Nov 26, 2013 at 10:01:14AM +, Alan Gauld wrote: Is there a method to compare a substring, without building a substring >from the big one? Like startswith or endswith, but anywhere inside the string? test = s[1, -1] == "bcd"#

Re: [Tutor] string replacement in Python 2 and 3

2013-11-27 Thread Albert-Jan Roskam
On Wed, 11/27/13, Steven D'Aprano wrote: Subject: Re: [Tutor] string replacement in Python 2 and 3 To: tutor@python.org Date: Wednesday, November 27, 2013, 12:36 AM On Tue, Nov 26, 2013 at 11:42:29AM -0800, Albert-Jan Roskam wrote:

Re: [Tutor] string replacement in Python 2 and 3

2013-11-26 Thread Steven D'Aprano
On Tue, Nov 26, 2013 at 11:42:29AM -0800, Albert-Jan Roskam wrote: > Hi, > > String replacement works quite differently with bytes objects in > Python 3 than with string objects in Python 2. What is the best way to > make example #1 below run in Python 2 and 3? If you are working with text str

Re: [Tutor] string codes

2013-11-26 Thread Danny Yoo
Hi Denis, For reference, you can explore the documentation to find out what strings can do: http://docs.python.org/3/library/stdtypes.html#text-sequence-type-str > What is the method to get a code or list of codes inside a string: > s = "abcde" > c = s.code(2) > ass

Re: [Tutor] string codes

2013-11-26 Thread eryksun
On Tue, Nov 26, 2013 at 6:34 AM, Steven D'Aprano wrote: > > I think that views would be useful for *very large strings*, but very > large probably means a lot larger than you might think. For small > strings, say under a few hundred or perhaps even thousand characters, > making a copy of the subst

Re: [Tutor] string codes

2013-11-26 Thread ALAN GAULD
Pleae use ReplyAll to include the list. > c = ord(s[2]) > >Yes, that's it: i forgot about Python's builtin functions, only searched among >methods. Then, two more questions: >-1- Why isn't this a str method? s.ord() [or better s.code()] looks natural, >doesn't it?Because it operates on a single

Re: [Tutor] string codes

2013-11-26 Thread Alan Gauld
On 26/11/13 11:59, Steven D'Aprano wrote: test = s.startswith("bcd", 1, -1) That doesn't work, unfortunately: py> s = "abcdZZZ" py> s[1:-1] == "bcd" False py> s.startswith("bcd", 1, -1) True Oops. You'd have to do both startswith() and endswith() tests, and even then it doesn't work:

Re: [Tutor] string codes

2013-11-26 Thread Steven D'Aprano
On Tue, Nov 26, 2013 at 10:01:14AM +, Alan Gauld wrote: > >Is there a method to compare a substring, without building a substring > >from the big one? Like startswith or endswith, but anywhere inside the > >string? > > test = s[1, -1] == "bcd"# no!, builds a substring > > I assume you

  1   2   3   4   >