Skip to main content

Python - String manipulation

To learn basic string manipulations in Python, you can try the examples below. I hope you find this useful.

Reminder: If you are an expert in Python then you can skip over article and still stay tuned for more articles on software engineering.

strings-example.py
___________________________________________________
#!/usr/bin/python
def main():
    s = 'this is a string'

    print(s.capitalize())
    print(s.title())
    print(s.upper())
    print(s.swapcase())
    print(s.find('is'))
    print(s.replace('this', 'that'))
    print(s.strip())
    print(s.isalnum())
    print(s.isalpha())
    print(s.isdigit())
    print(s.startswith("this"))
    pos = s.find('str', 1, 15)
    if pos == -1:
        print("did NOT find it")
    elif pos == 10:
        print ("found it at 10")
    else:
        print("found it")

    print("++++++++++++++++++++++++")

    str = "Hello World!"    print(str[2:])   # llo World!

    print(str[2:5])    # llo

    print(str[:7])     # Hello W

    print(str[:-2])    # Hello Worl

    print(str[-2:])    # d!

    print(str[2:-2])   # llo Worl

    print(str[::-1])   # this reverses the whole string

    print(str[::2])    # this skips characters going forward: HloWrd



if __name__ == "__main__": main()
___________________________________________________



Output:
___________________________________________________
This is a string
This Is A String
THIS IS A STRING
THIS IS A STRING
2
that is a string
this is a string
False
False
False
True
found it at 10
++++++++++++++++++++++++
llo World!
llo
Hello W
Hello Worl
d!
llo Worl
!dlroW olleH
HloWrd

___________________________________________________


Thank you for reading this post.

Almir Mustafic

Comments

Popular posts from this blog

Daylight saving time and A Software Engineering state of mind ?

You may be wondering what the Daylight saving time has to do with a software engineering state of mind. When thinking about writing this article, at first I thought to start with the following joke and I am: “ Did you know that the Daylight saving time was started because a software developer coded a function that does smart timezone and configurable calculations and then this developer created a problem to solve to use the algorithm; hence, the Daylight saving time was born. ” This is a joke, but  on a more serious note , this brings me to a state of mind in software engineering that make this joke a reality to some degree. How many times did we find ourselves in situations where we learned something new in programming and we looked for ways to apply it at any cost? How many times did we see a cool new feature from a creator of a framework and we decided to use it even though that was not the right solution for the problem or maybe there was no problem to solve in the ...

Leaders/Mentors in my life

I have been blessed in my software engineering career with great leaders. Some of them challenged me in technical skills. Some of them challenged me in my organization and leadership skills. Some of them challenged me in both. And all of them made me a better software engineer, a better senior engineer, a better solutions architect, a better teammate, and a better leader. If you are a student, find yourself a mentor. If you are a junior software engineer, find yourself a mentor. If you are an experienced software engineer, find yourself a mentor. Remember, you write your own definition of success and you are your own critic. That may mean that you TRY to perfect every stage of your career, or that may mean that you skip some stages in your career. Remember, you are in control. That’s all I wanted to say today :) Keep geeking out. Almir

OWNING your sandbox

OWNING your sandbox. As software engineers we all like to work on latest technology and coding new applications. People generally don’t like spending a lot of time maintaining the code. However, in the world of microservices the owners of each microservice are very well known and defined unlike in the world of monolithic applications. That means that you own it in the true sense. You own the code. You own the QA environment. You own the Stage environment. You own the production environment and all the errors that come along with it :) On positive note, you have a lot to be proud of and you can turn it into opportunities :) You own something that is contributing your company’s customers and what you do responsibly affects the lives of many people in a positive way. Almir Mustafic