I am a beginner. This is my code with the outcome in bold where the words are joined together. Question is how do I separate them after print?
adjective = input("Enter an adjective: ")
noun = input("Enter a noun: ")
verb = input("Enter a verb phrase: ")
adverb = input("Enter an adverb phrase: ")
sentence = "The " + adjective + noun + verb + adverb
print(sentence)
The funnyfarmerate crabsat the dentist
this should do it:
sentence = "The " +adjective +" " +noun +" " +verb +" " +adverb +"."
print(sentence)
I hope your having fun learning python :)
Related
I have tried all day to replace this character I have in a string which has characters in it... and after googling, I found is called a "Control Sequence Introducer".
It looks like the hex code is 9B and the ASCII code is 155. (I think, from what I've read).
The string comes from a file which I read in, I have some null characters to replace, which is working fine, but just after that I've been working to remove this wierd character.
In notepad++ when I do show all symbols, it looks like this:
I tried the following:
strLine = strLine.Replace(Chr(155), " ")
strLine = Replace(strLine , "9B", " ")
strLine = Replace(strLine , "›", " ")
strLine = Replace(strLine , Chr(155), " ")
strLine= Regex.Replace(strLine, "\c#", " ")
strLine= Regex.Replace(strLine, "\c_", " ")
strLine= Regex.Replace(strLine, "\c", " ")
strLine= Regex.Replace(strLine, "\cA", " ")
strLine= Regex.Replace(strLine, "\cZ", " ")
I found a good section in wikipedia
Control Sequence Introducer Character ANSI Control Sequences
With everything going on in that, perhaps I have the wrong hex code?
Does anybody know how to replace this character? I have googled this for awhile now and it's elusive to me how to solve this.
I did find it finally, using regex and the hex code!
strLine = Regex.Replace(strLine, "\x9B", " ")
Can someone please guide which one of the below is the correct Data Annotation, if I want to allow just alphabets:
[RegularExpression(#"[a-zA-Z]*", ErrorMessage = "Invalid {0}")]
OR
[RegularExpression(#"^[a-zA-Z]*", ErrorMessage = "Invalid {0}")]
Both seems to be working. The difference is ^ symbol.
^ Caret is a Position Anchor.
Position Anchors does not match character, but position such as start-of-line, end-of-line, start-of-word and end-of-word.
In this case you need both ^ and $: start-of-line and end-of-line respectively. E.g., ^[0-9]$ matches a numeric string.
So you should go with,
[RegularExpression(#"^[a-zA-Z]*$", ErrorMessage = "Invalid {0}")]
Becase you need strings starts and ends with alphabetical characters only, not having any other characters such as symbols or numerals. Here are some examples that you can play with.
let str1 = 'abcDef';
let str2 = '123abcDef';
let str3 = 'abcDef123';
let str4 = 'abc123Def';
let my_regex = /^[a-zA-Z]*$/;
let your_regex = /[a-zA-Z]*/;
alert(str1 + " : " + my_regex.test(str1) + " with my regex");
alert(str2 + " : " + my_regex.test(str2) + " with my regex");
alert(str3 + " : " + my_regex.test(str3) + " with my regex");
alert(str4 + " : " + my_regex.test(str4) + " with my regex");
alert(str1 + " : " + your_regex.test(str1) + " with your regex");
alert(str2 + " : " + your_regex.test(str2) + " with your regex");
alert(str3 + " : " + your_regex.test(str3) + " with your regex");
alert(str4 + " : " + your_regex.test(str4) + " with your regex");
The Caret (^) character is also referred to by the following terms:
Terminology
hat, control, uparrow, chevron, circumflex accent
Usage
It has two uses in regular expressions:
To denote the start of the line
If used immediately after a square bracket ([^) it acts to negate the set of allowed characters (i.e. [123] means the character 1, 2, or 3 is allowed, whilst the statement [^123] means any character other than 1, 2, or 3 is allowed.
Character Escaping
To express a caret without special meaning, it should be escaped by preceding it with a backslash; i.e. ^.
You can find it here...
I think you want something more like this:
[RegularExpression(#"^[a-zA-Z]+$", ErrorMessage = "Invalid {0}")]
which says to match
the beginning of the string ^
followed by 1 or more alphabetic characters [a-zA-Z]+ (you do need more than zero right)?
followed by the end of the string $
This doesn't allow other strings to match such as 123abc or abc123 because the anchors of ^ and $ prevent that.
In your first example, the match would allow an empty string, and would allow for the cases I mentioned in the paragraph above. Your second example would allow empty string, but would at least filter out 123abc but would still allow abc123 because you don't have the $ marker.
If you want to take my solution and extend it beyond ASCII alphabetic characters, you can change [a-ZA-Z]+ to \p{L}+, which should work universally in Unicode (but that seems like it might be more than you're looking for; just including for completeness).
Finally, [RegularExpression] uses the standard regex capability that has been part of .NET for quite some time, expressed in the Regular Expression Language - Quick Reference.
I have a form (say) Data, which contains text boxes A,B,C.
I wish to write an email based on the form data. In my email body, I want the following format:
A is : (actual value of A in text box in the form) (a newline)
B is : ((actual value of B in text box in the form) ( a newline)
C is : ((actual value of C in text box in the form).
I know I can access values by Forms!Data!A_value (assuming I named the box as A_value). I am not able to combine them into a string and add a newline too.
I have tried the following:
Dim body as String
body = "A is : & Forms!Data!A_value &" & "B is : & Forms!Data!B_value &" & "C is : & Forms!Data!C_value &"
It is because I read an & results to a new line somewhere.
However, when i do that, the whole thing is concatenated as written in the code and no values are obtained from the form field.
Please suggest options:
Thanks in advance
You probably want something like:
Dim body as String
body = "A is : " & Forms!Data!A_value & vbNewLine & _
"B is : " & Forms!Data!B_value & vbNewLine & _
"C is : " & Forms!Data!C_value
Note: the fact that I wrote that code on 3 lines, using line continuation characters, is nothing to do with the insertion of the new line characters in the output. It could have also been written as
body = "A is : " & Forms!Data!A_value & vbNewLine & "B is : " & Forms!Data!B_value & vbNewLine & "C is : " & Forms!Data!C_value
but I find that harder to read.
The ampersand is used to concatenate text "hello " & "there".
If you quote the ampersand it does nothing, just reproduces the ampersand "bits & bobs".
You can use the character vbCrLf (carriage return/linefeed) to add (concatenate) a linebreak, "time for " & vbCrLf & "a break".
Another trick you can use is to create a single string template with placeholders for the values, then use Replace statements to fill them in, like:
body = "A is: {A} & B is: {B} & C is: {C}"
body = Replace(body, "{A}", Forms!Data!A_value)
body = Replace(body, "{B}", Forms!Data!B_value)
body = Replace(body, "{C}", Forms!Data!C_value)
And break out to multiple lines, like:
body = "A is: {A}{CR}B is: {B}{CR}C is: {C}{CR}"
body = Replace(body, "{A}", Forms!Data!A_value)
body = Replace(body, "{B}", Forms!Data!B_value)
body = Replace(body, "{C}", Forms!Data!C_value)
body = Replace(body, "{CR}", vbCrLf)
So my code is like this :
richtextbox1.text = x1.text + x2.text (x's are labels)
And the result is like this :
x1x2
Expected Output :
x1
x2
You have to use NewLine Char to put extra line between two string.
richTextBox1.Text = string1 + System.Environment.NewLine + string2
Hi guys I am not good at this but this worked for me:
1. declare string 1 and string 2 just before the if statement like this using Dim:
Dim string1 = "I am teacher."
Dim string2 = "I like my students."
If TextBox1.Text = "write here the word that you want to search" Then
RichTextBox1.Text = "I am teacher." + System.Environment.NewLine + "I like my students."
End If
I am teacher. is an example of a paragraph1
I like my students. is an example of a paragraph2
Now here is the if statement with the two paragraphs above:
Try this
RichTextbox1.Text = x1.Text & " " &
Issue, where the character I am removing does not exist I get a blank string
Aim: To look for three characters in order and only get the characters to the left of the character I am looking for. However if the character does not exist then to do nothing.
Code:
Dim vleftString As String = File.Name
vleftString = Left(vleftString, InStr(vleftString, "-"))
vleftString = Left(vleftString, InStr(vleftString, "_"))
vleftString = Left(vleftString, InStr(vleftString, " "))
As a 'fix' I have done
Dim vleftString As String = File.Name
vleftString = Replace(vleftString, "-", " ")
vleftString = Replace(vleftString, "_", " ")
vleftString = Left(vleftString, InStr(vleftString, " "))
vleftString = Trim(vleftString)
Based on Left of a character in a string in vb.net
If File.Name is say 1_2.pdf it passes "-" and then works on line removing anything before "" (though not "" though I want it to)
When it hits the line for looking for anything left of space it then makes vleftString blank.
Since i'm not familiar (and avoid) the old VB functions here a .NET approach. I assume you want to remove the parts behind the separators "-", "_" and " ", then you can use this loop:
Dim fileName = "1_2.pdf".Trim() ' Trim used to show you the method, here nonsense
Dim name = Path.GetFileNameWithoutExtension(fileName).Trim()
For Each separator In {"-", "_", " "}
Dim index = name.IndexOf(separator)
If index >= 0 Then
name = name.Substring(0, index)
End If
Next
fileName = String.Format("{0}{1}", name, Path.GetExtension(fileName))
Result: "1.pdf"