Working With Strings In Python
In this tutorial we are going to take a look at how you work with strings in Python. Now, any language worth its salt will have a number of options for working with text and Python is probably one of the best to use when it comes to processing text.
If you are new to programming in general you may be wondering what a string is. In terms of programming, a string is classed as any sequence of characters you can type with your keyboard, and let’s face it, if you want your application to be of any use to yourself or other users then you need it to tell you what it’s doing or to prompt you for an action, and that is where strings come into play.
They are your applications way of communicating with the user. Without the ability to enter and display text or software would be pretty useless.
So, how would you create a string in Python? Take a look at the following code:
myString = “Hi there. I am a string”
First we create a variable called myString. Next we use the equals operator to give it the value of the text enclosed in double quotes.
If you wanted to display that text when your application is run, then you would use the print statement like so:
print myString
Seems easy enough so far, right? There are of course other ways you can work with textual data. For example, if you wanted to join two strings you would use the plus operator as shown in the code below:
stringOne = “I am string one. “
stringTwo = “I am string two. “
myString = stringOne + stringTwo
In the above example if you were to use the print statement on the myString variable you would get: I am string one. I am string two.
Python also comes with functions which can make working on strings very easy. If you wanted to find the number of times a certain word appears within a string you could use the count function like so:
myString = “This is a string. This is a string”
stringCount = myString.count(”This”)
print stringCount
In the example above we create out variable and assign it some text. On line two we create another variable to hold the value of the count function. You will notice that we give the count function a word (another string). What we are doing there is telling it to check the original string for the number of times that word appears. Lastly we print out our results, which if you followed along and used the same code as above should be two.
This is just a taster of the string functions available to you. There are many others which perform a multitude of tasks such as capitalizing all words in a string, and there are others for splitting a string into tokens and much more. Python has a function for pretty much anything you would want to do with strings.
Don’t be afraid to have a play around with them, experimentation is a great way to learn any new language.
other blog entries
Course Directory [training on all levels]
- .NET Classes
- Agile/Scrum Classes
- Ajax Classes
- Android and iPhone Programming Classes
- Blaze Advisor Classes
- C Programming Classes
- C# Programming Classes
- C++ Programming Classes
- Cisco Classes
- Cloud Classes
- CompTIA Classes
- Crystal Reports Classes
- Design Patterns Classes
- DevOps Classes
- Foundations of Web Design & Web Authoring Classes
- Git, Jira, Wicket, Gradle, Tableau Classes
- IBM Classes
- Java Programming Classes
- JBoss Administration Classes
- JUnit, TDD, CPTC, Web Penetration Classes
- Linux Unix Classes
- Machine Learning Classes
- Microsoft Classes
- Microsoft Development Classes
- Microsoft SQL Server Classes
- Microsoft Team Foundation Server Classes
- Microsoft Windows Server Classes
- Oracle, MySQL, Cassandra, Hadoop Database Classes
- Perl Programming Classes
- Python Programming Classes
- Ruby Programming Classes
- Security Classes
- SharePoint Classes
- SOA Classes
- Tcl, Awk, Bash, Shell Classes
- UML Classes
- VMWare Classes
- Web Development Classes
- Web Services Classes
- Weblogic Administration Classes
- XML Classes
- Introduction to Spring 5 (2022)
16 December, 2024 - 18 December, 2024 - VMware vSphere 8.0 Boot Camp
9 December, 2024 - 13 December, 2024 - Introduction to C++ for Absolute Beginners
16 December, 2024 - 17 December, 2024 - RED HAT ENTERPRISE LINUX AUTOMATION WITH ANSIBLE
2 December, 2024 - 5 December, 2024 - Ruby Programming
2 December, 2024 - 4 December, 2024 - See our complete public course listing
did you know? HSG is one of the foremost training companies in the United States
Our courses focus on two areas: the most current and critical object-oriented and component based tools, technologies and languages; and the fundamentals of effective development methodology. Our programs are designed to deliver technology essentials while improving development staff productivity.
An experienced trainer and faculty member will identify the client's individual training requirements, then adapt and tailor the course appropriately. Our custom training solutions reduce time, risk and cost while keeping development teams motivated. The Hartmann Software Group's faculty consists of veteran software engineers, some of whom currently teach at several Colorado Universities. Our faculty's wealth of knowledge combined with their continued real world consulting experience enables us to produce more effective training programs to ensure our clients receive the highest quality and most relevant instruction available. Instruction is available at client locations or at various training facilities located in the metropolitan Denver area.
Upcoming Classes
- Introduction to Spring 5 (2022)
16 December, 2024 - 18 December, 2024 - VMware vSphere 8.0 Boot Camp
9 December, 2024 - 13 December, 2024 - Introduction to C++ for Absolute Beginners
16 December, 2024 - 17 December, 2024 - RED HAT ENTERPRISE LINUX AUTOMATION WITH ANSIBLE
2 December, 2024 - 5 December, 2024 - Ruby Programming
2 December, 2024 - 4 December, 2024 - See our complete public course listing
consulting services we do what we know ... write software
The coaching program integrates our course instruction with hands on software development practices. By employing XP (Extreme Programming) techniques, we teach students as follows:
Configure and integrate the needed development tools
MOntitor each students progress and offer feedback, perspective and alternatives when needed.
Establish an Action plan to yield a set of deliverables in order to guarantee productive learning.
Establish an Commit to a deliverable time line.
Hold each student accountable to a standard that is comparable to that of an engineer/project manager with at least one year's experience in the field.
These coaching cycles typically last 2-4 weeks in duration.
Business Rule isolation and integration for large scale systems using Blaze Advisor
Develop Java, .NET, Perl, Python, TCL and C++ related technologies for Web, Telephony, Transactional i.e. financial and a variety of other considerations.
Windows and Unix/Linux System Administration.
Application Server Administration, in particular, Weblogic, Oracle and JBoss.
Desperate application communication by way of Web Services (SOAP & Restful), RMI, EJBs, Sockets, HTTP, FTP and a number of other protocols.
Graphics Rich application development work i.e. fat clients and/or Web Clients to include graphic design
Performance improvement through code rewrites, code interpreter enhancements, inline and native code compilations and system alterations.
Mentoring of IT and Business Teams for quick and guaranteed expertise transfer.
Architect both small and large software development systems to include: Data Dictionaries, UML Diagrams, Software & Systems Selections and more