Managing Strings in Python

Response to question: https://stackoverflow.com/questions/48605323/i-have-a-list-of-popular-nouns-in-english

There are multiple ways to create string variables and output strings. Some offer more flexibility than others and may lend themselves better than others depending on the need. See the code below written in Python 3.6:

import os

#Example 1: single string variable separated by spaces

words = "Aaardvark Abucus Able"

print(words)

#Example 2: multiple string variables with a space between them

word1 = "Aaardvark"

word2 = "Abucus"

word3 = "Able"

print(word1 + " " + word2 + " " + word3)

#Example 3: list of string variables. 

words=["Aaardvark", "Abucus", "Able"]

print(words, end='')

Example #1 is probably your simplest method. However, this isn’t really flexible as you have just hard coded the text into a variable.

Example #2 allows you to group multiple text variables together to allow for a bit more flexibility.

Example #3 allows you to create a list of string variables and then output their contents in list format.

 

%d bloggers like this: