Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8432083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:01:40+00:00 2026-06-10T06:01:40+00:00

I currently have the following code, which finds capital letters in a string ‘formula’:

  • 0

I currently have the following code, which finds capital letters in a string ‘formula’: http://pastebin.com/syRQnqCP

Now, my question is, how can I alter that code (Disregard the bit within the “if choice = 1:” loop) so that each part of that newly broken up string is put into it’s own variable?

For example, putting in NaBr would result in the string being broken into “Na” and “Br”. I need to put those in separate variables so I can look them up in my CSV file.
Preferably it’d be a kind of generated thing, so if there are 3 elements, like MgSO4, O would be put into a separate variable like Mg and S would be.

If this is unclear, let me know and I’ll try and make it a bit more comprehensible… No way of doing so comes to mind currently, though. 🙁

EDIT: Relevant pieces of code:

Function:

def split_uppercase(string):
x=''
for i in string: 
    if i.isupper(): x+=' %s' %i 
    else: x+=i 
return x.strip()

String entry and lookup:

formula = raw_input("Enter formula: ")
upper = split_uppercase(formula)

#Pull in data from form.csv
weight1 = float(formul_data.get(element1.lower()))
weight2 = float(formul_data.get(element2.lower()))
weight3 = float(formul_data.get(element3.lower()))


weightSum = weight1 + weight2 + weight3
print "Total weight =", weightSum
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:01:42+00:00Added an answer on June 10, 2026 at 6:01 am

    I think there is a far easier way to do what you’re trying to do. Use regular expressions. For instance:

    >>> [a for a in re.split(r'([A-Z][a-z]*)', 'MgSO4') if a]
    ['Mg', u'S', u'O', u'4']
    

    If you want the number attached to the right element, just add a digit specifier in the regex:

    >>> [a for a in re.split(r'([A-Z][a-z]*\d*)', txt) if a]
    [u'Mg', u'S', u'O4']
    

    You don’t really want to “put each part in its own variable”. That doesn’t make sense in general, because you don’t know how many parts there are, so you can’t know how many variables to create ahead of time. Instead, you want to make a list, like in the example above. Then you can iterate over this list and do what you need to do with each piece.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have the following code which delays showing a fixed bar after a
I currently have the following code which uses a ZipInputStream instance to decompress a
I have this following code which will return all the current semesters. How do
Currently I have the following code to flash a DataGrid cell when it's value
I currently have the following js code function clearMulti(option) { var i; var select
The following code shows what I currently have. It is an adapter for circular
I have the following code currently: <DataTemplate DataType={x:Type vm:SectionViewModel}> <ScrollViewer> <ItemsControl ItemsSource={Binding ViewModels}> </ItemsControl>
I am currently working on Problem 62 I have tried the following code to
I currently have code that does the following: private final static ExecutorService pool =
I have the following function which retrives the currently logged in users' username and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.