SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Aug 2008
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using loop and indexing to get specific letters in a string.

    Not sure if anyone lives here, hopefully there is someone watching.

    I created the following tuple. How do I use indexing to get, example: All the months that starts with the letter J?

    Here is what I have.

    Code:
    monthsOfYear = ("January", "February", "March", "April", "May", "June",\
    "July", "August", "September", "October", "November", "December")
    for monthsOfYear in monthsOfYear:
            print monthsOfYear[:j]
    I am stuck.

    Hopefully someone stops in here.

    Thanks

    IC

  2. #2
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,276
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Since this area is a mush of a bunch of languages (the LeftOvers I call them), it's prolly a good idea to state which language you're using too (this looks like Python right?).

    All I can do is make an idjit uneducated guess until someone else comes along:
    Code:
    for months in monthsOfYear:
            print months[:j]
    ?
    I mean in other languages it's
    "for singleThing in groupOfThings {
    print singleThing[index];
    }"

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code Python:
    import calendar
     
    for i in range(1, 13):
        month = calendar.month_name[i]  #get month as a string
     
        if month[0] == "J":  #check first character of string
            print month
     
    --output:--
    January
    June
    July

  4. #4
    SitePoint Addict
    Join Date
    Aug 2008
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud View Post
    Code Python:
    import calendar
     
    for i in range(1, 13):
        month = calendar.month_name[i]  #get month as a string
     
        if month[0] == "J":  #check first character of string
            print month
     
    --output:--
    January
    June
    July
    Thanks very much!!

    IC


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •