How can I do line breaks in a formatted string string python3 - python-3.x

simple question that I hope has a simple answer
I want to store a formatted string to pass later to function call. I would like to have line breaks baked into one long string rather than send a string for each line. So for example
b = f'hello \n world'
b
this gives me 'hello \n world' which is not what I want.
How can I do this in a compact manner. I would prefer to avoid joins or something like that with a newline separator.
Thanks in advance.

Related

Manipulation with strings: Capitalize every word after any delimiter in Python

I am trying to manipulate string so it capitalizes each word after any delimiter.
Currently, I am using capwords() method imported from string module. Code sample:
my_string = "hello MY-naMe-is john"
new_string = string.capwords(my_string)
print(new_string)
Using only capwords() method, result is this:
Hello My-name-is John
Result that I am trying to get:
Hello My-Name-Is John
Is it possible to use more than one separator in capwords()? Is there a solution to this while still using capwords() method? Thanks!
Use title() in built method
my_string = "hello MY-naMe-is john"
new_string = my_string.title()
print(new_string)
#'Hello My-Name-Is John'
There is no way to use capwords and get the result that you need. The capwords(s, sep=None) documentation shows the steps that it undergoes:
str.split() - here the sep is used (if provided)
str.capitalize() - first character of each item in the list (from split) is capitalized
str.join() - the string is joined back
split takes just one sep. However, re.split takes multiple delimeters/separators.
Use title() if you want to achieve your desired result, as suggested by #Ananth.P

string parts seperated by ; to ASCII written in a new string

Something like that is coming in:
str="Hello;this;is;a;text"
What I do want as result is this:
result="72:101:108:108:111;116:104:105:115;..."
which should be the Text in ASCII.
You could use string matching to get each word separated by ; and then convert, concat:
local str = "Hello;this;is;a;text"
for word in str:gmatch("[^;]+") do
ascii = table.pack(word:byte(1, -1))
local converted = table.concat(ascii, ":")
print(converted)
end
The output of the above code is:
72:101:108:108:111
116:104:105:115
105:115
97
116:101:120:116
I'll leave the rest of work to you. Hint: use table.concat.
Here is another approach, which exploits that fact that gsub accepts a table where it reads replacements:
T={}
for c=0,255 do
T[string.char(c)]=c..":"
end
T[";"]=";"
str="Hello;this;is;a;text"
result=str:gsub(".",T):gsub(":;",";")
print(result)
Another possibility:
function convert(s)
return (s:gsub('.',function (s)
if s == ';' then return s end
return s:byte()..':'
end)
:gsub(':;',';')
:gsub(':$',''))
end
print(convert 'Hello;this;is;a;text')
Finding certain character or string (such as ";") can be done by using string.find - https://www.lua.org/pil/20.1.html
Converting character to its ASCII code can be done by string.byte - https://www.lua.org/pil/20.html
What you need to do is build a new string using two functions mentioned above. If you need more string-based functions please visit official Lua site: https://www.lua.org/pil/contents.html
Okay...I got way further, but I can't find how to return a string made up of two seperate strings like
str=str1&" "&str2

rstrip() has no effect on string

Trying to use rstrip() at its most basic level, but it does not seem to have any effect at all.
For example:
string1='text&moretext'
string2=string1.rstrip('&')
print(string2)
Desired Result:
text
Actual Result:
text&moretext
Using Python 3, PyScripter
What am I missing?
someString.rstrip(c) removes all occurences of c at the end of the string. Thus, for example
'text&&&&'.rstrip('&') = 'text'
Perhaps you want
'&'.join(string1.split('&')[:-1])
This splits the string on the delimiter "&" into a list of strings, removes the last one, and joins them again, using the delimiter "&". Thus, for example
'&'.join('Hello&World'.split('&')[:-1]) = 'Hello'
'&'.join('Hello&Python&World'.split('&')[:-1]) = 'Hello&Python'

Reading from a string using sscanf in Matlab

I'm trying to read a string in a specific format
RealSociedad
this is one example of string and what I want to extract is the name of the team.
I've tried something like this,
houseteam = sscanf(str, '%s');
but it does not work, why?
You can use regexprep like you did in your post above to do this for you. Even though your post says to use sscanf and from the comments in your post, you'd like to see this done using regexprep. You would have to do this using two nested regexprep calls, and you can retrieve the team name (i.e. RealSociedad) like so, given that str is in the format that you have provided:
str = 'RealSociedad';
houseteam = regexprep(regexprep(str, '^<a(.*)">', ''), '</a>$', '')
This looks very intimidating, but let's break this up. First, look at this statement:
regexprep(str, '^<a(.*)">', '')
How regexprep works is you specify the string you want to analyze, the pattern you are searching for, then what you want to replace this pattern with. The pattern we are looking for is:
^<a(.*)">
This says you are looking for patterns where the beginning of the string starts with a a<. After this, the (.*)"> is performing a greedy evaluation. This is saying that we want to find the longest sequence of characters until we reach the characters of ">. As such, what the regular expression will match is the following string:
<ahref="/teams/spain/real-sociedad-de-futbol/2028/">
We then replace this with a blank string. As such, the output of the first regexprep call will be this:
RealSociedad</a>
We want to get rid of the </a> string, and so we would make another regexprep call where we look for the </a> at the end of the string, then replace this with the blank string yet again. The pattern you are looking for is thus:
</a>$
The dollar sign ($) symbolizes that this pattern should appear at the end of the string. If we find such a pattern, we will replace it with the blank string. Therefore, what we get in the end is:
RealSociedad
Found a solution. So, %s stops when it finds a space.
str = regexprep(str, '<', ' <');
str = regexprep(str, '>', '> ');
houseteam = sscanf(str, '%*s %s %*s');
This will create a space between my desired string.

Is there a way to add quotes to a multi paragraph string

I wrote the following line:
string QuoteTest2 = "Benjamin Netnayahu,\"BB\", said that: \"Israel will not fall\"";
This example went well, but what can I do in case I want to write a multi paragraph string including quotes?
The following example shows that puting '#' before the doesn't cut it..
string QuoteTest2 = #"Benjamin Netnayahu,\"BB\", said that: \"Israel will not fall\"";
The string ends and the second quote and the over just gives me errors, what should I do?
Use double quotes to escape ""
e.g.
string QuoteTest2 = #"Benjamin Netnayahu,""BB"", said that: ""Israel will not fall""";

Resources