We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,434 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

String Splitting Problem

Hello,
Can someone please help me with a little problem that I am having splitting strings?
I am trying to remove everything more than 2 - digits to the right of a decimal point in a string.
For some reason I am not realizing my desired results. If you could just give me a few hints without giving me the answer I would greatly appreciate it.
Thank You,
HR

# This program demonstrates the split method.

def main():
   # Create a string with multiple words.
   my_string = '1247.123456789'

   # Split the string.
   number_list = my_string.split('.')
   secondString = number_list[1:2]

   secondString = secondString[0 : 2]
   print(secondString)

# Call the main function.
main()
4
Contributors
7
Replies
11 Hours
Discussion Span
11 Months Ago
Last Updated
8
Views
HankReardon
Light Poster
37 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

secondString = number_list[1:2] will give you a list that contains the second string as its only element. Then when you do secondString[0:2] you're saying "take the first two elements of this list", but the list only has one element, so you get the same list back unchanged.

What you want is for secondString to be the string directly and not a list containing the string. So you should be doing secondString = number_list[1].

sepp2k
Posting Whiz in Training
244 posts since Jul 2012
Reputation Points: 76
Solved Threads: 45
Skill Endorsements: 8

Thanks sepp2k. I will play around with this.
HR

HankReardon
Light Poster
37 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This program demonstrates the split method.

def main():

Create a string with multiple words.

my_string = '1247.123456789'

Split the string.

number_list = my_string.split('.')
secondString = number_list[1:2]
secondString = secondString[0 : 2]
print(secondString)

Call the main function.

main()

HankReardon
Light Poster
37 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Hello sepp2k,
It works just like you said it would, thanks.
It still strikes me as odd that when I printed secondString right after the statement
secondString = number_list[1:2]
I would get the second string.

I guess it's still just one element. For instance when I replace [1:2] with [0] I will get the first two digits in the first string of numbers.

I really do appreciate this little tutorial sepp2k.
HR

HankReardon
Light Poster
37 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Hope this solves this, if your desired output is 1247.12:

# This program demonstrates the split method.

def main():
# Create a string with multiple words.

    my_string = '1247.123456789'

# Split the string.   

    number_list = my_string.split('.')  
    print number_list

    firstString = number_list[0]
    print firstString
    secondString = number_list[1]
    secondString = secondString[0 : 2]   
    print(secondString)

    final_string = firstString + '.' + secondString
    print final_string
# Call the main function.

main()
peterparker
Light Poster
32 posts since Jul 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

There is also

my_string[:my_string.index('.') + 3]

or

"{0:.2f}".format(float(my_string))

but the program no longer illustrates the split() method.

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

Peter Parker's code is exactly how I did mine last night. Griboullis's code is what I was striving for but could not figure out.
Thanks to all.

HankReardon
Light Poster
37 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0865 seconds using 2.74MB