Code to split aws region and zone

This is the place for queries that don't fit in any of the other categories.

Code to split aws region and zone

Postby roadrage » Wed Sep 28, 2016 8:26 pm

Hi ,
I have the following task to create a function 'aws_split_zone', taking a single string argument (AWSzone): The function must:
1) return two substrings:
AWS region
AWS zone identifier

example
aws_split_zone('eu-west-1a') -> 'eu-west-1'. 'a'

I have written the following script but it does not seem to work
Code: Select all
import sys
for arg in sys.argv:
    print arg

def aws_split_zone(arg):
        aws_region = arg.rstrip('0123456789')
        aws_zone = arg.lstrip('0123456789')
        print('aws region is:', aws_region)
        print('aws zone is:', aws_zone)

aws_split_zone(arg)
Last edited by ichabod801 on Wed Sep 28, 2016 8:30 pm, edited 1 time in total.
Reason: First post lock
roadrage
 
Posts: 1
Joined: Wed Sep 28, 2016 8:22 pm

Re: Code to split aws region and zone

Postby ichabod801 » Wed Sep 28, 2016 8:36 pm

The rstrip and lstrip methods are for removing characters from a string, not for splitting strings. It looks like AWS regions are everything but the last character, so you just need to split the last character from the rest of the string. You could do that with slicing.
Due to the reasons discussed here we will be moving to python-forum.io on October 1st, 2016.
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
ichabod801
 
Posts: 688
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: Code to split aws region and zone

Postby Skaperen » Thu Sep 29, 2016 1:19 am

as ichabod801 points out, it is as simple as slicing.
Code: Select all
lt1/forums /home/forums 5> py
Python 2.7.12 (default, Jul  1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a='us-east-1a'
>>> a[-1]
'a'
>>> a[:-1]
'us-east-1'
>>>
lt1/forums /home/forums 6> py3
Python 3.5.2 (default, Sep 10 2016, 08:21:44)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a='eu-central-1b'
>>> a[-1]
'b'
>>> a[:-1]
'eu-central-1'
>>>
lt1/forums /home/forums 7>
Due to the reasons discussed here we will be moving to python-forum.io on October 1st, 2016. This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
Skaperen
 
Posts: 267
Joined: Tue Apr 07, 2015 9:58 am
Location: Wheeling WV USA


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 9 guests